@guillaume-docquier/tools-ts 7.5.2 → 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -13
- package/dist/Brand.d.ts +21 -5
- package/dist/Brand.js.map +1 -1
- package/package.json +12 -13
package/README.md
CHANGED
|
@@ -69,19 +69,19 @@ pseudo-random sequences.
|
|
|
69
69
|
|
|
70
70
|
These exports are type-only helpers for common TypeScript modeling problems.
|
|
71
71
|
|
|
72
|
-
| Export | What it is
|
|
73
|
-
| ------------------------------------------ |
|
|
74
|
-
| `Branded
|
|
75
|
-
| `Unbranded` | Removes a brand from a type.
|
|
76
|
-
| `UnbrandedProperties` | Removes brands from an object's property types.
|
|
77
|
-
| `ConstructorType` | A type for class constructors.
|
|
78
|
-
| `EnumKeyType`, `EnumValueType`, `EnumType` | Broad shapes for enum-like objects.
|
|
79
|
-
| `Enumify` | Converts a const object into a union of its values.
|
|
80
|
-
| `ValueOf` | Produces a union of an object's value types.
|
|
81
|
-
| `Prettify` | Re-expands an object type for easier editor hovers.
|
|
82
|
-
| `OmitOverUnion` | Applies `Omit` distributively across union members.
|
|
83
|
-
| `PartialProperties` | Makes selected object properties partial, not the whole object.
|
|
84
|
-
| `Mutable` | Removes `readonly` from selected properties.
|
|
72
|
+
| Export | What it is | Use it when |
|
|
73
|
+
| ------------------------------------------ | ---------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------ |
|
|
74
|
+
| `Branded<TBrand, TType>`, `branded` | A composable nominal typing helper plus a runtime no-op for branding values. | Structurally identical values need distinct domain types. |
|
|
75
|
+
| `Unbranded` | Removes a brand from a type. | An API accepts the underlying value of a branded type. |
|
|
76
|
+
| `UnbrandedProperties` | Removes brands from an object's property types. | An object input should use raw values for branded fields. |
|
|
77
|
+
| `ConstructorType` | A type for class constructors. | A function accepts a class and later uses `new` or `instanceof` against it. |
|
|
78
|
+
| `EnumKeyType`, `EnumValueType`, `EnumType` | Broad shapes for enum-like objects. | You are writing generic utilities that operate on TypeScript enums or const-object enums. |
|
|
79
|
+
| `Enumify` | Converts a const object into a union of its values. | A project avoids TypeScript `enum` but wants enum-like ergonomics from `as const` objects. |
|
|
80
|
+
| `ValueOf` | Produces a union of an object's value types. | You need the values of a lookup object as a type. |
|
|
81
|
+
| `Prettify` | Re-expands an object type for easier editor hovers. | Intersections or mapped types are correct but hard to read in tooling. |
|
|
82
|
+
| `OmitOverUnion` | Applies `Omit` distributively across union members. | Native `Omit` collapses a discriminated union in a way that loses member-specific fields. |
|
|
83
|
+
| `PartialProperties` | Makes selected object properties partial, not the whole object. | Only nested property objects should become partial while the parent shape stays required. |
|
|
84
|
+
| `Mutable` | Removes `readonly` from selected properties. | You have a narrow, deliberate mutation need. Avoid this unless there is no cleaner model. |
|
|
85
85
|
|
|
86
86
|
### Logging
|
|
87
87
|
|
package/dist/Brand.d.ts
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
declare const brand: unique symbol;
|
|
2
2
|
declare const baseType: unique symbol;
|
|
3
|
-
type AnyBrand =
|
|
3
|
+
type AnyBrand = {
|
|
4
|
+
[baseType]: unknown;
|
|
5
|
+
};
|
|
4
6
|
type TypeOf<TBrand extends AnyBrand> = TBrand[typeof baseType];
|
|
5
7
|
/**
|
|
6
8
|
* Type utility to create branded types.
|
|
7
9
|
*
|
|
8
10
|
* @example
|
|
9
11
|
* ```ts
|
|
10
|
-
* type UserId = Branded<
|
|
12
|
+
* type UserId = Branded<"UserId", string>
|
|
11
13
|
* function getUser(userId: UserId): User {}
|
|
12
14
|
*
|
|
13
15
|
* const str = "a string"
|
|
@@ -16,9 +18,21 @@ type TypeOf<TBrand extends AnyBrand> = TBrand[typeof baseType];
|
|
|
16
18
|
* getUser(str) // S2345: Argument of type string is not assignable to parameter of type UserId
|
|
17
19
|
* getUser(userId) // works!
|
|
18
20
|
* ```
|
|
21
|
+
*
|
|
22
|
+
* Brands can be composed with a normal type intersection.
|
|
23
|
+
*
|
|
24
|
+
* ```ts
|
|
25
|
+
* type PositiveNumber = Branded<"PositiveNumber", number>
|
|
26
|
+
* type Integer = Branded<"Integer", number>
|
|
27
|
+
* type PositiveInteger = PositiveNumber & Integer
|
|
28
|
+
*
|
|
29
|
+
* const positiveNumber: PositiveNumber = branded<PositiveInteger>(1)
|
|
30
|
+
* const integer: Integer = branded<PositiveInteger>(1)
|
|
31
|
+
* const positiveInteger: PositiveInteger = branded<PositiveInteger>(1)
|
|
32
|
+
* ```
|
|
19
33
|
*/
|
|
20
|
-
export type Branded<
|
|
21
|
-
[brand]: TBrand;
|
|
34
|
+
export type Branded<TBrand, TType> = TType & {
|
|
35
|
+
readonly [brand]: (value: TBrand) => TBrand;
|
|
22
36
|
} & {
|
|
23
37
|
[baseType]: TType;
|
|
24
38
|
};
|
|
@@ -31,7 +45,9 @@ export declare function branded<TBrand extends AnyBrand>(value: TypeOf<TBrand>):
|
|
|
31
45
|
* Removes the brand from a branded type while leaving other types unchanged.
|
|
32
46
|
* Usually useful for tests when you want to accept unbranded arguments and brand them to alleviate test code.
|
|
33
47
|
*/
|
|
34
|
-
export type Unbranded<T> = T extends
|
|
48
|
+
export type Unbranded<T> = T extends {
|
|
49
|
+
[baseType]: infer Base;
|
|
50
|
+
} ? Base : T;
|
|
35
51
|
/**
|
|
36
52
|
* Removes brands from an object's property types.
|
|
37
53
|
* Usually useful for tests when you want to accept unbranded arguments and brand them to alleviate test code.
|
package/dist/Brand.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Brand.js","sourceRoot":"","sources":["../src/Brand.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Brand.js","sourceRoot":"","sources":["../src/Brand.ts"],"names":[],"mappings":"AAmCA;;;GAGG;AACH,MAAM,UAAU,OAAO,CAA0B,KAAqB;IACpE,6HAA6H;IAC7H,OAAO,KAAe,CAAA;AACxB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@guillaume-docquier/tools-ts",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.0",
|
|
4
4
|
"description": "My personal collection of typescript tools",
|
|
5
5
|
"homepage": "https://github.com/Guillaume-Docquier/tools-ts",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,30 +25,29 @@
|
|
|
25
25
|
"provenance": true
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@logtape/logtape": "^2.3.
|
|
29
|
-
"@logtape/redaction": "^2.3.
|
|
28
|
+
"@logtape/logtape": "^2.3.3",
|
|
29
|
+
"@logtape/redaction": "^2.3.3"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@guillaume-docquier/oxfmt": "^1.0.
|
|
33
|
-
"@guillaume-docquier/oxlint": "^1.8.
|
|
32
|
+
"@guillaume-docquier/oxfmt": "^1.0.8",
|
|
33
|
+
"@guillaume-docquier/oxlint": "^1.8.1",
|
|
34
34
|
"@semantic-release/commit-analyzer": "^13.0.1",
|
|
35
35
|
"@semantic-release/exec": "^7.1.0",
|
|
36
36
|
"@semantic-release/github": "^12.0.9",
|
|
37
37
|
"@semantic-release/release-notes-generator": "^14.1.1",
|
|
38
|
-
"@types/node": "^
|
|
39
|
-
"@vitest/coverage-v8": "^
|
|
38
|
+
"@types/node": "^26.4.1",
|
|
39
|
+
"@vitest/coverage-v8": "^5.0.0",
|
|
40
40
|
"conventional-changelog-conventionalcommits": "^9.3.1",
|
|
41
41
|
"husky": "^9.1.7",
|
|
42
42
|
"jiti": "^2.7.0",
|
|
43
|
-
"lint-staged": "^
|
|
44
|
-
"oxfmt": "
|
|
45
|
-
"oxlint": "^1.
|
|
43
|
+
"lint-staged": "^17.5.0",
|
|
44
|
+
"oxfmt": ">=0.67.0",
|
|
45
|
+
"oxlint": "^1.82.0",
|
|
46
46
|
"oxlint-tsgolint": "^7.0.2001",
|
|
47
47
|
"semantic-release": "^25.0.9",
|
|
48
48
|
"typescript": "~7.0.2",
|
|
49
49
|
"vite": "^8.2.2",
|
|
50
|
-
"vitest": "^
|
|
51
|
-
"node": "runtime:26.8.1"
|
|
50
|
+
"vitest": "^5.0.0"
|
|
52
51
|
},
|
|
53
52
|
"lint-staged": {
|
|
54
53
|
"*.{md,json,yml,yaml,css,scss}": "oxfmt --no-error-on-unmatched-pattern",
|
|
@@ -60,7 +59,7 @@
|
|
|
60
59
|
"devEngines": {
|
|
61
60
|
"packageManager": {
|
|
62
61
|
"name": "pnpm",
|
|
63
|
-
"version": "
|
|
62
|
+
"version": "12.3.4",
|
|
64
63
|
"onFail": "download"
|
|
65
64
|
},
|
|
66
65
|
"runtime": {
|