type-fest 2.13.0 → 2.15.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 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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-fest",
3
- "version": "2.13.0",
3
+ "version": "2.15.0",
4
4
  "description": "A collection of essential TypeScript types",
5
5
  "license": "(MIT OR CC0-1.0)",
6
6
  "repository": "sindresorhus/type-fest",
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.
@@ -1,14 +1,9 @@
1
1
  import type {JsonPrimitive, JsonValue} from './basic';
2
+ import {Finite, NegativeInfinity, PositiveInfinity} from './numeric';
3
+ import {TypedArray} from './typed-array';
2
4
 
3
5
  // Note: The return value has to be `any` and not `unknown` so it can match `void`.
4
- type NotJsonable = ((...args: any[]) => any) | undefined;
5
-
6
- // Note: Handles special case where Arrays with `undefined` are transformed to `'null'` by `JSON.stringify()`
7
- // Only use with array members
8
- type JsonifyArrayMember<T> =
9
- T extends undefined ?
10
- null | Exclude<T, undefined> :
11
- Jsonify<T>;
6
+ type NotJsonable = ((...args: any[]) => any) | undefined | symbol;
12
7
 
13
8
  /**
14
9
  Transform a type to one that is assignable to the `JsonValue` type.
@@ -71,16 +66,26 @@ type Jsonify<T> =
71
66
  // Check if there are any non-JSONable types represented in the union.
72
67
  // Note: The use of tuples in this first condition side-steps distributive conditional types
73
68
  // (see https://github.com/microsoft/TypeScript/issues/29368#issuecomment-453529532)
74
- [Extract<T, NotJsonable>] extends [never]
75
- ? T extends JsonPrimitive
69
+ [Extract<T, NotJsonable | BigInt>] extends [never]
70
+ ? T extends PositiveInfinity | NegativeInfinity ? null
71
+ : T extends JsonPrimitive
76
72
  ? T // Primitive is acceptable
73
+ : T extends Number ? number
74
+ : T extends String ? string
75
+ : T extends Boolean ? boolean
76
+ : T extends Map<any, any> | Set<any> ? {}
77
+ : T extends TypedArray ? Record<string, number>
77
78
  : T extends Array<infer U>
78
- ? Array<JsonifyArrayMember<U>> // It's an array: recursive call for its children
79
+ ? Array<Jsonify<U extends NotJsonable ? null : U>> // It's an array: recursive call for its children
79
80
  : T extends object
80
81
  ? T extends {toJSON(): infer J}
81
82
  ? (() => J) extends (() => JsonValue) // Is J assignable to JsonValue?
82
83
  ? J // Then T is Jsonable and its Jsonable value is J
83
84
  : never // Not Jsonable because its toJSON() method does not return JsonValue
84
- : {[P in keyof T]: Jsonify<Required<T>[P]>} // It's an object: recursive call for its children
85
+ : {[P in keyof T as P extends symbol
86
+ ? never
87
+ : T[P] extends NotJsonable
88
+ ? never
89
+ : P]: Jsonify<Required<T>[P]>} // It's an object: recursive call for its children
85
90
  : never // Otherwise any other non-object is removed
86
91
  : never; // Otherwise non-JSONable type union was found not empty
@@ -34,6 +34,8 @@ You can't pass a `bigint` as they are already guaranteed to be finite.
34
34
 
35
35
  Use-case: Validating and documenting parameters.
36
36
 
37
+ Note: This can't detect `NaN`, please upvote [this issue](https://github.com/microsoft/TypeScript/issues/28682) if you want to have this type as a built-in in TypeScript.
38
+
37
39
  @example
38
40
  ```
39
41
  import type {Finite} from 'type-fest';
@@ -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;
@@ -713,7 +713,7 @@ declare namespace TsConfigJson {
713
713
  /**
714
714
  Specify path mapping to be computed relative to baseUrl option.
715
715
  */
716
- paths?: Partial<Record<string, string[]>>;
716
+ paths?: Record<string, string[]>;
717
717
 
718
718
  /**
719
719
  List of TypeScript language server plugins to load.
package/license DELETED
@@ -1 +0,0 @@
1
- SPDX-License-Identifier: (MIT OR CC0-1.0)