type-fest 2.13.1 → 2.14.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/index.d.ts +1 -1
- package/package.json +1 -1
- package/readme.md +1 -0
- package/source/opaque.d.ts +33 -0
package/index.d.ts
CHANGED
|
@@ -18,7 +18,7 @@ export {PartialDeep} from './source/partial-deep';
|
|
|
18
18
|
export {ReadonlyDeep} from './source/readonly-deep';
|
|
19
19
|
export {LiteralUnion} from './source/literal-union';
|
|
20
20
|
export {Promisable} from './source/promisable';
|
|
21
|
-
export {Opaque} from './source/opaque';
|
|
21
|
+
export {Opaque, UnwrapOpaque} from './source/opaque';
|
|
22
22
|
export {InvariantOf} from './source/invariant-of';
|
|
23
23
|
export {SetOptional} from './source/set-optional';
|
|
24
24
|
export {SetRequired} from './source/set-required';
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -172,6 +172,7 @@ Click the type names for complete docs.
|
|
|
172
172
|
- [`ReadonlyDeep`](source/readonly-deep.d.ts) - Create a deeply immutable version of an `object`/`Map`/`Set`/`Array` type. Use [`Readonly<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#readonlytype) if you only need one level deep.
|
|
173
173
|
- [`LiteralUnion`](source/literal-union.d.ts) - Create a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union. Workaround for [Microsoft/TypeScript#29729](https://github.com/Microsoft/TypeScript/issues/29729).
|
|
174
174
|
- [`Opaque`](source/opaque.d.ts) - Create an [opaque type](https://codemix.com/opaque-types-in-javascript/).
|
|
175
|
+
- [`UnwrapOpaque`](source/opaque.d.ts) - Revert an [opaque type](https://codemix.com/opaque-types-in-javascript/) back to its original type.
|
|
175
176
|
- [`InvariantOf`](source/invariant-of.d.ts) - Create an [invariant type](https://basarat.gitbook.io/typescript/type-system/type-compatibility#footnote-invariance), which is a type that does not accept supertypes and subtypes.
|
|
176
177
|
- [`SetOptional`](source/set-optional.d.ts) - Create a type that makes the given keys optional.
|
|
177
178
|
- [`SetRequired`](source/set-required.d.ts) - Create a type that makes the given keys required.
|
package/source/opaque.d.ts
CHANGED
|
@@ -72,3 +72,36 @@ type Person = {
|
|
|
72
72
|
@category Type
|
|
73
73
|
*/
|
|
74
74
|
export type Opaque<Type, Token = unknown> = Type & Tagged<Token>;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
Revert an opaque type back to its original type by removing the readonly `[tag]`.
|
|
78
|
+
|
|
79
|
+
Why is this necessary?
|
|
80
|
+
|
|
81
|
+
1. Use an `Opaque` type as object keys
|
|
82
|
+
2. Prevent TS4058 error: "Return type of exported function has or is using name X from external module Y but cannot be named"
|
|
83
|
+
|
|
84
|
+
@example
|
|
85
|
+
```
|
|
86
|
+
import type {Opaque, UnwrapOpaque} from 'type-fest';
|
|
87
|
+
|
|
88
|
+
type AccountType = Opaque<'SAVINGS' | 'CHECKING', 'AccountType'>;
|
|
89
|
+
|
|
90
|
+
const moneyByAccountType: Record<UnwrapOpaque<AccountType>, number> = {
|
|
91
|
+
SAVINGS: 99,
|
|
92
|
+
CHECKING: 0.1
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// Without UnwrapOpaque, the following expression would throw a type error.
|
|
96
|
+
const money = moneyByAccountType.SAVINGS; // TS error: Property 'SAVINGS' does not exist
|
|
97
|
+
|
|
98
|
+
// Attempting to pass an non-Opaque type to UnwrapOpaque will raise a type error.
|
|
99
|
+
type WontWork = UnwrapOpaque<string>;
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
@category Type
|
|
103
|
+
*/
|
|
104
|
+
export type UnwrapOpaque<OpaqueType extends Tagged<unknown>> =
|
|
105
|
+
OpaqueType extends Opaque<infer Type, OpaqueType[typeof tag]>
|
|
106
|
+
? Type
|
|
107
|
+
: OpaqueType;
|