@fluidframework/core-interfaces 2.61.0 → 2.62.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/.mocharc.cjs +13 -1
- package/CHANGELOG.md +4 -0
- package/dist/brandedType.d.ts +57 -4
- package/dist/brandedType.d.ts.map +1 -1
- package/dist/brandedType.js.map +1 -1
- package/lib/brandedType.d.ts +57 -4
- package/lib/brandedType.d.ts.map +1 -1
- package/lib/brandedType.js.map +1 -1
- package/package.json +3 -3
- package/src/brandedType.ts +57 -4
- package/test-config.json +7 -0
package/.mocharc.cjs
CHANGED
|
@@ -7,4 +7,16 @@
|
|
|
7
7
|
|
|
8
8
|
// @fluid-internal/mocha-test-setup depends on this package, so we can't use it.
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
const testCJS = process.env.FLUID_TEST_MODULE_SYSTEM === "CJS";
|
|
11
|
+
const outputFilePrefix = testCJS ? "CJS-" : "";
|
|
12
|
+
const suiteName = "@fluidframework/core-interfaces" + (testCJS ? " - CJS" : "");
|
|
13
|
+
module.exports = {
|
|
14
|
+
spec: testCJS ? "dist/test/**/*.spec.*js" : "lib/test/**/*.spec.*js",
|
|
15
|
+
recursive: true,
|
|
16
|
+
require: [testCJS ? "./dist/test/mochaHooks.js" : "./lib/test/mochaHooks.js"],
|
|
17
|
+
reporter: "mocha-multi-reporters",
|
|
18
|
+
"reporter-options": [
|
|
19
|
+
`configFile=test-config.json,cmrOutput=xunit+output+${outputFilePrefix}:xunit+suiteName+${suiteName}`,
|
|
20
|
+
],
|
|
21
|
+
"unhandled-rejections": "strict",
|
|
22
|
+
};
|
package/CHANGELOG.md
CHANGED
package/dist/brandedType.d.ts
CHANGED
|
@@ -6,15 +6,68 @@
|
|
|
6
6
|
* Base branded type which can be used to annotate other type.
|
|
7
7
|
*
|
|
8
8
|
* @remarks
|
|
9
|
-
*
|
|
10
|
-
*
|
|
9
|
+
* `BrandedType` is covariant over its type parameter, which could be leveraged
|
|
10
|
+
* for any generic type, but the preferred pattern is to specify variance
|
|
11
|
+
* explicitly in a derived class making that clear and guaranteeing branding is
|
|
12
|
+
* unique. It is convenient to have the derived class be the generic given to
|
|
13
|
+
* `BrandedType`.
|
|
14
|
+
*
|
|
15
|
+
* ### Direct use [simple]
|
|
16
|
+
*
|
|
17
|
+
* Use `T & BrandedType<"BrandName">` to create a type conforming to `T` and
|
|
18
|
+
* also branded with name "BrandName".
|
|
19
|
+
*
|
|
20
|
+
* ### Derived class use [preferred]
|
|
21
|
+
*
|
|
22
|
+
* Derive another class declaration and ideally add additional
|
|
23
|
+
* protected properties to distinguish the type. (Private properties would
|
|
24
|
+
* {@link https://github.com/microsoft/TypeScript/issues/20979#issuecomment-361432516|lose their type when exported}
|
|
25
|
+
* and public properties would allow structural typing and show up on the branded
|
|
26
|
+
* values.)
|
|
27
|
+
*
|
|
28
|
+
* Then use `T & MyBrandedType<U>` to create a type conforming to `T` and
|
|
29
|
+
* also branded with the derived brand.
|
|
30
|
+
*
|
|
31
|
+
* ### Runtime
|
|
11
32
|
*
|
|
12
33
|
* Since branded types are not real value types, they will always need to be
|
|
13
|
-
* created using `as` syntax and
|
|
34
|
+
* created using `as` syntax and often `as unknown` first.
|
|
14
35
|
*
|
|
15
36
|
* This class should never exist at runtime, so it is only declared.
|
|
16
37
|
*
|
|
17
|
-
* @
|
|
38
|
+
* @example
|
|
39
|
+
* Definition of two branded types with different variance:
|
|
40
|
+
* ```typescript
|
|
41
|
+
* // A brand that is covariant over given T
|
|
42
|
+
* declare class CovariantBrand<T> extends BrandedType<CovariantBrand<unknown>> {
|
|
43
|
+
* // Does not allow unrelated or less derived CovariantBrand-ed types to be
|
|
44
|
+
* // assigned. CovariantBrand<string> is not assignable to CovariantBrand<"literal">.
|
|
45
|
+
* protected readonly CovariantBrand: T;
|
|
46
|
+
* private constructor();
|
|
47
|
+
* }
|
|
48
|
+
* // A brand that is contravariant over given T
|
|
49
|
+
* declare class ContravariantBrand<T> extends BrandedType<ContravariantBrand<unknown>> {
|
|
50
|
+
* // Does not allow unrelated or more derived ContravariantBrand-ed types to be
|
|
51
|
+
* // assigned. ContravariantBrand<"literal"> is not assignable to ContravariantBrand<string>.
|
|
52
|
+
* protected readonly ContravariantBrand: (_: T) => void;
|
|
53
|
+
* private constructor();
|
|
54
|
+
* }
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* Applying a brand to a type through type-guard:
|
|
58
|
+
* ```typescript
|
|
59
|
+
* function numberIs5(n: number): n is number & CovariantBrand<5> {
|
|
60
|
+
* return n === 5;
|
|
61
|
+
* }
|
|
62
|
+
* function onlyAccept4_5_or_6(_n: number & CovariantBrand<4 | 5 | 6>): void {}
|
|
63
|
+
*
|
|
64
|
+
* function example(n: number) {
|
|
65
|
+
* if (numberIs5(n)) {
|
|
66
|
+
* onlyAccept4_5_or_6(n); // OK: CovariantBrand<5> is assignable to CovariantBrand<4 | 5 | 6>;
|
|
67
|
+
* }
|
|
68
|
+
* }
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
18
71
|
* @internal
|
|
19
72
|
*/
|
|
20
73
|
export declare class BrandedType<out Brand> {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"brandedType.d.ts","sourceRoot":"","sources":["../src/brandedType.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH
|
|
1
|
+
{"version":3,"file":"brandedType.d.ts","sourceRoot":"","sources":["../src/brandedType.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmEG;AACH,MAAM,CAAC,OAAO,OAAO,WAAW,CAAC,GAAG,CAAC,KAAK;IACzC;;;;;;;;;OASG;IACH,SAAS,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,KAAK,CAAC;IAElD,SAAS;IAET;;;;;OAKG;WACW,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,IAAI,KAAK;CAChE"}
|
package/dist/brandedType.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"brandedType.js","sourceRoot":"","sources":["../src/brandedType.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * Base branded type which can be used to annotate other type.\n *\n * @remarks\n *
|
|
1
|
+
{"version":3,"file":"brandedType.js","sourceRoot":"","sources":["../src/brandedType.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * Base branded type which can be used to annotate other type.\n *\n * @remarks\n * `BrandedType` is covariant over its type parameter, which could be leveraged\n * for any generic type, but the preferred pattern is to specify variance\n * explicitly in a derived class making that clear and guaranteeing branding is\n * unique. It is convenient to have the derived class be the generic given to\n * `BrandedType`.\n *\n * ### Direct use [simple]\n *\n * Use `T & BrandedType<\"BrandName\">` to create a type conforming to `T` and\n * also branded with name \"BrandName\".\n *\n * ### Derived class use [preferred]\n *\n * Derive another class declaration and ideally add additional\n * protected properties to distinguish the type. (Private properties would\n * {@link https://github.com/microsoft/TypeScript/issues/20979#issuecomment-361432516|lose their type when exported}\n * and public properties would allow structural typing and show up on the branded\n * values.)\n *\n * Then use `T & MyBrandedType<U>` to create a type conforming to `T` and\n * also branded with the derived brand.\n *\n * ### Runtime\n *\n * Since branded types are not real value types, they will always need to be\n * created using `as` syntax and often `as unknown` first.\n *\n * This class should never exist at runtime, so it is only declared.\n *\n * @example\n * Definition of two branded types with different variance:\n * ```typescript\n * // A brand that is covariant over given T\n * declare class CovariantBrand<T> extends BrandedType<CovariantBrand<unknown>> {\n * // Does not allow unrelated or less derived CovariantBrand-ed types to be\n * // assigned. CovariantBrand<string> is not assignable to CovariantBrand<\"literal\">.\n * protected readonly CovariantBrand: T;\n * private constructor();\n * }\n * // A brand that is contravariant over given T\n * declare class ContravariantBrand<T> extends BrandedType<ContravariantBrand<unknown>> {\n * // Does not allow unrelated or more derived ContravariantBrand-ed types to be\n * // assigned. ContravariantBrand<\"literal\"> is not assignable to ContravariantBrand<string>.\n * protected readonly ContravariantBrand: (_: T) => void;\n * private constructor();\n * }\n * ```\n *\n * Applying a brand to a type through type-guard:\n * ```typescript\n * function numberIs5(n: number): n is number & CovariantBrand<5> {\n * return n === 5;\n * }\n * function onlyAccept4_5_or_6(_n: number & CovariantBrand<4 | 5 | 6>): void {}\n *\n * function example(n: number) {\n * if (numberIs5(n)) {\n * onlyAccept4_5_or_6(n); // OK: CovariantBrand<5> is assignable to CovariantBrand<4 | 5 | 6>;\n * }\n * }\n * ```\n *\n * @internal\n */\nexport declare class BrandedType<out Brand> {\n\t/**\n\t * Compile time only marker to make type checking more strict.\n\t * This method will not exist at runtime and accessing it is invalid.\n\t *\n\t * @privateRemarks\n\t * `Brand` is used as the return type of a method rather than a simple\n\t * readonly property as this allows types with two brands to be\n\t * intersected without getting `never`.\n\t * The method takes in `never` to help emphasize that it's not callable.\n\t */\n\tprotected readonly brand: (dummy: never) => Brand;\n\n\tprotected constructor();\n\n\t/**\n\t * Since this class is a compile time only type brand, `instanceof` will\n\t * never work with it. * This `Symbol.hasInstance` implementation ensures\n\t * that `instanceof` will error if used, and in TypeScript 5.3 and newer\n\t * will produce a compile time error if used.\n\t */\n\tpublic static [Symbol.hasInstance](value: never): value is never;\n}\n"]}
|
package/lib/brandedType.d.ts
CHANGED
|
@@ -6,15 +6,68 @@
|
|
|
6
6
|
* Base branded type which can be used to annotate other type.
|
|
7
7
|
*
|
|
8
8
|
* @remarks
|
|
9
|
-
*
|
|
10
|
-
*
|
|
9
|
+
* `BrandedType` is covariant over its type parameter, which could be leveraged
|
|
10
|
+
* for any generic type, but the preferred pattern is to specify variance
|
|
11
|
+
* explicitly in a derived class making that clear and guaranteeing branding is
|
|
12
|
+
* unique. It is convenient to have the derived class be the generic given to
|
|
13
|
+
* `BrandedType`.
|
|
14
|
+
*
|
|
15
|
+
* ### Direct use [simple]
|
|
16
|
+
*
|
|
17
|
+
* Use `T & BrandedType<"BrandName">` to create a type conforming to `T` and
|
|
18
|
+
* also branded with name "BrandName".
|
|
19
|
+
*
|
|
20
|
+
* ### Derived class use [preferred]
|
|
21
|
+
*
|
|
22
|
+
* Derive another class declaration and ideally add additional
|
|
23
|
+
* protected properties to distinguish the type. (Private properties would
|
|
24
|
+
* {@link https://github.com/microsoft/TypeScript/issues/20979#issuecomment-361432516|lose their type when exported}
|
|
25
|
+
* and public properties would allow structural typing and show up on the branded
|
|
26
|
+
* values.)
|
|
27
|
+
*
|
|
28
|
+
* Then use `T & MyBrandedType<U>` to create a type conforming to `T` and
|
|
29
|
+
* also branded with the derived brand.
|
|
30
|
+
*
|
|
31
|
+
* ### Runtime
|
|
11
32
|
*
|
|
12
33
|
* Since branded types are not real value types, they will always need to be
|
|
13
|
-
* created using `as` syntax and
|
|
34
|
+
* created using `as` syntax and often `as unknown` first.
|
|
14
35
|
*
|
|
15
36
|
* This class should never exist at runtime, so it is only declared.
|
|
16
37
|
*
|
|
17
|
-
* @
|
|
38
|
+
* @example
|
|
39
|
+
* Definition of two branded types with different variance:
|
|
40
|
+
* ```typescript
|
|
41
|
+
* // A brand that is covariant over given T
|
|
42
|
+
* declare class CovariantBrand<T> extends BrandedType<CovariantBrand<unknown>> {
|
|
43
|
+
* // Does not allow unrelated or less derived CovariantBrand-ed types to be
|
|
44
|
+
* // assigned. CovariantBrand<string> is not assignable to CovariantBrand<"literal">.
|
|
45
|
+
* protected readonly CovariantBrand: T;
|
|
46
|
+
* private constructor();
|
|
47
|
+
* }
|
|
48
|
+
* // A brand that is contravariant over given T
|
|
49
|
+
* declare class ContravariantBrand<T> extends BrandedType<ContravariantBrand<unknown>> {
|
|
50
|
+
* // Does not allow unrelated or more derived ContravariantBrand-ed types to be
|
|
51
|
+
* // assigned. ContravariantBrand<"literal"> is not assignable to ContravariantBrand<string>.
|
|
52
|
+
* protected readonly ContravariantBrand: (_: T) => void;
|
|
53
|
+
* private constructor();
|
|
54
|
+
* }
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* Applying a brand to a type through type-guard:
|
|
58
|
+
* ```typescript
|
|
59
|
+
* function numberIs5(n: number): n is number & CovariantBrand<5> {
|
|
60
|
+
* return n === 5;
|
|
61
|
+
* }
|
|
62
|
+
* function onlyAccept4_5_or_6(_n: number & CovariantBrand<4 | 5 | 6>): void {}
|
|
63
|
+
*
|
|
64
|
+
* function example(n: number) {
|
|
65
|
+
* if (numberIs5(n)) {
|
|
66
|
+
* onlyAccept4_5_or_6(n); // OK: CovariantBrand<5> is assignable to CovariantBrand<4 | 5 | 6>;
|
|
67
|
+
* }
|
|
68
|
+
* }
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
18
71
|
* @internal
|
|
19
72
|
*/
|
|
20
73
|
export declare class BrandedType<out Brand> {
|
package/lib/brandedType.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"brandedType.d.ts","sourceRoot":"","sources":["../src/brandedType.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH
|
|
1
|
+
{"version":3,"file":"brandedType.d.ts","sourceRoot":"","sources":["../src/brandedType.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmEG;AACH,MAAM,CAAC,OAAO,OAAO,WAAW,CAAC,GAAG,CAAC,KAAK;IACzC;;;;;;;;;OASG;IACH,SAAS,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,KAAK,CAAC;IAElD,SAAS;IAET;;;;;OAKG;WACW,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,IAAI,KAAK;CAChE"}
|
package/lib/brandedType.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"brandedType.js","sourceRoot":"","sources":["../src/brandedType.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * Base branded type which can be used to annotate other type.\n *\n * @remarks\n *
|
|
1
|
+
{"version":3,"file":"brandedType.js","sourceRoot":"","sources":["../src/brandedType.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * Base branded type which can be used to annotate other type.\n *\n * @remarks\n * `BrandedType` is covariant over its type parameter, which could be leveraged\n * for any generic type, but the preferred pattern is to specify variance\n * explicitly in a derived class making that clear and guaranteeing branding is\n * unique. It is convenient to have the derived class be the generic given to\n * `BrandedType`.\n *\n * ### Direct use [simple]\n *\n * Use `T & BrandedType<\"BrandName\">` to create a type conforming to `T` and\n * also branded with name \"BrandName\".\n *\n * ### Derived class use [preferred]\n *\n * Derive another class declaration and ideally add additional\n * protected properties to distinguish the type. (Private properties would\n * {@link https://github.com/microsoft/TypeScript/issues/20979#issuecomment-361432516|lose their type when exported}\n * and public properties would allow structural typing and show up on the branded\n * values.)\n *\n * Then use `T & MyBrandedType<U>` to create a type conforming to `T` and\n * also branded with the derived brand.\n *\n * ### Runtime\n *\n * Since branded types are not real value types, they will always need to be\n * created using `as` syntax and often `as unknown` first.\n *\n * This class should never exist at runtime, so it is only declared.\n *\n * @example\n * Definition of two branded types with different variance:\n * ```typescript\n * // A brand that is covariant over given T\n * declare class CovariantBrand<T> extends BrandedType<CovariantBrand<unknown>> {\n * // Does not allow unrelated or less derived CovariantBrand-ed types to be\n * // assigned. CovariantBrand<string> is not assignable to CovariantBrand<\"literal\">.\n * protected readonly CovariantBrand: T;\n * private constructor();\n * }\n * // A brand that is contravariant over given T\n * declare class ContravariantBrand<T> extends BrandedType<ContravariantBrand<unknown>> {\n * // Does not allow unrelated or more derived ContravariantBrand-ed types to be\n * // assigned. ContravariantBrand<\"literal\"> is not assignable to ContravariantBrand<string>.\n * protected readonly ContravariantBrand: (_: T) => void;\n * private constructor();\n * }\n * ```\n *\n * Applying a brand to a type through type-guard:\n * ```typescript\n * function numberIs5(n: number): n is number & CovariantBrand<5> {\n * return n === 5;\n * }\n * function onlyAccept4_5_or_6(_n: number & CovariantBrand<4 | 5 | 6>): void {}\n *\n * function example(n: number) {\n * if (numberIs5(n)) {\n * onlyAccept4_5_or_6(n); // OK: CovariantBrand<5> is assignable to CovariantBrand<4 | 5 | 6>;\n * }\n * }\n * ```\n *\n * @internal\n */\nexport declare class BrandedType<out Brand> {\n\t/**\n\t * Compile time only marker to make type checking more strict.\n\t * This method will not exist at runtime and accessing it is invalid.\n\t *\n\t * @privateRemarks\n\t * `Brand` is used as the return type of a method rather than a simple\n\t * readonly property as this allows types with two brands to be\n\t * intersected without getting `never`.\n\t * The method takes in `never` to help emphasize that it's not callable.\n\t */\n\tprotected readonly brand: (dummy: never) => Brand;\n\n\tprotected constructor();\n\n\t/**\n\t * Since this class is a compile time only type brand, `instanceof` will\n\t * never work with it. * This `Symbol.hasInstance` implementation ensures\n\t * that `instanceof` will error if used, and in TypeScript 5.3 and newer\n\t * will produce a compile time error if used.\n\t */\n\tpublic static [Symbol.hasInstance](value: never): value is never;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fluidframework/core-interfaces",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.62.0",
|
|
4
4
|
"description": "Fluid object interfaces",
|
|
5
5
|
"homepage": "https://fluidframework.com",
|
|
6
6
|
"repository": {
|
|
@@ -143,8 +143,8 @@
|
|
|
143
143
|
"test": "npm run test:mocha",
|
|
144
144
|
"test:coverage": "c8 npm test",
|
|
145
145
|
"test:mocha": "npm run test:mocha:esm && echo skipping cjs to avoid overhead - npm run test:mocha:cjs",
|
|
146
|
-
"test:mocha:cjs": "mocha
|
|
147
|
-
"test:mocha:esm": "mocha
|
|
146
|
+
"test:mocha:cjs": "cross-env FLUID_TEST_MODULE_SYSTEM=CJS mocha",
|
|
147
|
+
"test:mocha:esm": "mocha",
|
|
148
148
|
"test:mocha:verbose": "cross-env FLUID_TEST_VERBOSE=1 npm run test:mocha",
|
|
149
149
|
"tsc": "fluid-tsc commonjs --project ./tsconfig.cjs.json && npm run place:cjs:package-stub",
|
|
150
150
|
"tsc:watch": "npm run place:cjs:package-stub && fluid-tsc commonjs --project ./tsconfig.cjs.json --watch",
|
package/src/brandedType.ts
CHANGED
|
@@ -7,15 +7,68 @@
|
|
|
7
7
|
* Base branded type which can be used to annotate other type.
|
|
8
8
|
*
|
|
9
9
|
* @remarks
|
|
10
|
-
*
|
|
11
|
-
*
|
|
10
|
+
* `BrandedType` is covariant over its type parameter, which could be leveraged
|
|
11
|
+
* for any generic type, but the preferred pattern is to specify variance
|
|
12
|
+
* explicitly in a derived class making that clear and guaranteeing branding is
|
|
13
|
+
* unique. It is convenient to have the derived class be the generic given to
|
|
14
|
+
* `BrandedType`.
|
|
15
|
+
*
|
|
16
|
+
* ### Direct use [simple]
|
|
17
|
+
*
|
|
18
|
+
* Use `T & BrandedType<"BrandName">` to create a type conforming to `T` and
|
|
19
|
+
* also branded with name "BrandName".
|
|
20
|
+
*
|
|
21
|
+
* ### Derived class use [preferred]
|
|
22
|
+
*
|
|
23
|
+
* Derive another class declaration and ideally add additional
|
|
24
|
+
* protected properties to distinguish the type. (Private properties would
|
|
25
|
+
* {@link https://github.com/microsoft/TypeScript/issues/20979#issuecomment-361432516|lose their type when exported}
|
|
26
|
+
* and public properties would allow structural typing and show up on the branded
|
|
27
|
+
* values.)
|
|
28
|
+
*
|
|
29
|
+
* Then use `T & MyBrandedType<U>` to create a type conforming to `T` and
|
|
30
|
+
* also branded with the derived brand.
|
|
31
|
+
*
|
|
32
|
+
* ### Runtime
|
|
12
33
|
*
|
|
13
34
|
* Since branded types are not real value types, they will always need to be
|
|
14
|
-
* created using `as` syntax and
|
|
35
|
+
* created using `as` syntax and often `as unknown` first.
|
|
15
36
|
*
|
|
16
37
|
* This class should never exist at runtime, so it is only declared.
|
|
17
38
|
*
|
|
18
|
-
* @
|
|
39
|
+
* @example
|
|
40
|
+
* Definition of two branded types with different variance:
|
|
41
|
+
* ```typescript
|
|
42
|
+
* // A brand that is covariant over given T
|
|
43
|
+
* declare class CovariantBrand<T> extends BrandedType<CovariantBrand<unknown>> {
|
|
44
|
+
* // Does not allow unrelated or less derived CovariantBrand-ed types to be
|
|
45
|
+
* // assigned. CovariantBrand<string> is not assignable to CovariantBrand<"literal">.
|
|
46
|
+
* protected readonly CovariantBrand: T;
|
|
47
|
+
* private constructor();
|
|
48
|
+
* }
|
|
49
|
+
* // A brand that is contravariant over given T
|
|
50
|
+
* declare class ContravariantBrand<T> extends BrandedType<ContravariantBrand<unknown>> {
|
|
51
|
+
* // Does not allow unrelated or more derived ContravariantBrand-ed types to be
|
|
52
|
+
* // assigned. ContravariantBrand<"literal"> is not assignable to ContravariantBrand<string>.
|
|
53
|
+
* protected readonly ContravariantBrand: (_: T) => void;
|
|
54
|
+
* private constructor();
|
|
55
|
+
* }
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* Applying a brand to a type through type-guard:
|
|
59
|
+
* ```typescript
|
|
60
|
+
* function numberIs5(n: number): n is number & CovariantBrand<5> {
|
|
61
|
+
* return n === 5;
|
|
62
|
+
* }
|
|
63
|
+
* function onlyAccept4_5_or_6(_n: number & CovariantBrand<4 | 5 | 6>): void {}
|
|
64
|
+
*
|
|
65
|
+
* function example(n: number) {
|
|
66
|
+
* if (numberIs5(n)) {
|
|
67
|
+
* onlyAccept4_5_or_6(n); // OK: CovariantBrand<5> is assignable to CovariantBrand<4 | 5 | 6>;
|
|
68
|
+
* }
|
|
69
|
+
* }
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
19
72
|
* @internal
|
|
20
73
|
*/
|
|
21
74
|
export declare class BrandedType<out Brand> {
|