type-fest 2.19.0 → 3.1.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.
Files changed (59) hide show
  1. package/index.d.ts +82 -73
  2. package/package.json +11 -10
  3. package/readme.md +26 -16
  4. package/source/async-return-type.d.ts +1 -3
  5. package/source/asyncify.d.ts +1 -2
  6. package/source/basic.d.ts +1 -1
  7. package/source/camel-case.d.ts +2 -2
  8. package/source/camel-cased-properties-deep.d.ts +5 -5
  9. package/source/camel-cased-properties.d.ts +3 -3
  10. package/source/conditional-except.d.ts +2 -2
  11. package/source/conditional-keys.d.ts +11 -11
  12. package/source/conditional-pick-deep.d.ts +102 -0
  13. package/source/conditional-pick.d.ts +2 -2
  14. package/source/conditional-simplify.d.ts +32 -0
  15. package/source/delimiter-case.d.ts +30 -24
  16. package/source/delimiter-cased-properties-deep.d.ts +6 -6
  17. package/source/delimiter-cased-properties.d.ts +2 -2
  18. package/source/empty-object.d.ts +46 -0
  19. package/source/enforce-optional.d.ts +47 -0
  20. package/source/entries.d.ts +3 -3
  21. package/source/entry.d.ts +3 -3
  22. package/source/exact.d.ts +4 -4
  23. package/source/fixed-length-array.d.ts +2 -2
  24. package/source/get.d.ts +52 -45
  25. package/source/has-optional-keys.d.ts +1 -1
  26. package/source/has-required-keys.d.ts +1 -1
  27. package/source/internal.d.ts +36 -0
  28. package/source/iterable-element.d.ts +4 -4
  29. package/source/join.d.ts +2 -2
  30. package/source/jsonify.d.ts +42 -20
  31. package/source/last-array-element.d.ts +4 -4
  32. package/source/literal-to-primitive.d.ts +12 -12
  33. package/source/merge-deep.d.ts +470 -0
  34. package/source/merge.d.ts +33 -10
  35. package/source/observable-like.d.ts +3 -2
  36. package/source/{remove-index-signature.d.ts → omit-index-signature.d.ts} +14 -11
  37. package/source/package-json.d.ts +51 -27
  38. package/source/partial-deep.d.ts +29 -31
  39. package/source/partial-on-undefined-deep.d.ts +12 -12
  40. package/source/pascal-cased-properties-deep.d.ts +5 -5
  41. package/source/pascal-cased-properties.d.ts +2 -2
  42. package/source/pick-index-signature.d.ts +102 -0
  43. package/source/readonly-deep.d.ts +14 -16
  44. package/source/require-all-or-none.d.ts +1 -1
  45. package/source/require-at-least-one.d.ts +4 -5
  46. package/source/schema.d.ts +22 -22
  47. package/source/set-non-nullable.d.ts +18 -9
  48. package/source/set-optional.d.ts +4 -4
  49. package/source/set-required.d.ts +4 -4
  50. package/source/simplify.d.ts +1 -26
  51. package/source/split.d.ts +2 -2
  52. package/source/spread.d.ts +4 -4
  53. package/source/stringified.d.ts +1 -1
  54. package/source/tsconfig-json.d.ts +16 -12
  55. package/source/tuple-to-union.d.ts +51 -0
  56. package/source/union-to-intersection.d.ts +3 -3
  57. package/source/writable.d.ts +4 -4
  58. package/source/mutable.d.ts +0 -5
  59. package/source/promise-value.d.ts +0 -29
@@ -28,8 +28,8 @@ type SomeOptional = SetOptional<Foo, 'b' | 'c'>;
28
28
  */
29
29
  export type SetOptional<BaseType, Keys extends keyof BaseType> =
30
30
  Simplify<
31
- // Pick just the keys that are readonly from the base type.
32
- Except<BaseType, Keys> &
33
- // Pick the keys that should be mutable from the base type and make them mutable.
34
- Partial<Pick<BaseType, Keys>>
31
+ // Pick just the keys that are readonly from the base type.
32
+ Except<BaseType, Keys> &
33
+ // Pick the keys that should be mutable from the base type and make them mutable.
34
+ Partial<Pick<BaseType, Keys>>
35
35
  >;
@@ -28,8 +28,8 @@ type SomeRequired = SetRequired<Foo, 'b' | 'c'>;
28
28
  */
29
29
  export type SetRequired<BaseType, Keys extends keyof BaseType> =
30
30
  Simplify<
31
- // Pick just the keys that are optional from the base type.
32
- Except<BaseType, Keys> &
33
- // Pick the keys that should be required from the base type and make them required.
34
- Required<Pick<BaseType, Keys>>
31
+ // Pick just the keys that are optional from the base type.
32
+ Except<BaseType, Keys> &
33
+ // Pick the keys that should be required from the base type and make them required.
34
+ Required<Pick<BaseType, Keys>>
35
35
  >;
@@ -1,23 +1,3 @@
1
- /**
2
- @see Simplify
3
- */
4
- export interface SimplifyOptions {
5
- /**
6
- Do the simplification recursively.
7
-
8
- @default false
9
- */
10
- deep?: boolean;
11
- }
12
-
13
- // Flatten a type without worrying about the result.
14
- type Flatten<
15
- AnyType,
16
- Options extends SimplifyOptions = {},
17
- > = Options['deep'] extends true
18
- ? {[KeyType in keyof AnyType]: Simplify<AnyType[KeyType], Options>}
19
- : {[KeyType in keyof AnyType]: AnyType[KeyType]};
20
-
21
1
  /**
22
2
  Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.
23
3
 
@@ -75,9 +55,4 @@ fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface`
75
55
 
76
56
  @category Object
77
57
  */
78
- export type Simplify<
79
- AnyType,
80
- Options extends SimplifyOptions = {},
81
- > = Flatten<AnyType> extends AnyType
82
- ? Flatten<AnyType, Options>
83
- : AnyType;
58
+ export type Simplify<T> = {[KeyType in keyof T]: T[KeyType]};
package/source/split.d.ts CHANGED
@@ -25,5 +25,5 @@ export type Split<
25
25
  > = S extends `${infer Head}${Delimiter}${infer Tail}`
26
26
  ? [Head, ...Split<Tail, Delimiter>]
27
27
  : S extends Delimiter
28
- ? []
29
- : [S];
28
+ ? []
29
+ : [S];
@@ -7,8 +7,8 @@ type SpreadObject<FirstType extends object, SecondType extends object> = {
7
7
  ? FirstType[Key] | Required<SecondType>[Key]
8
8
  : FirstType[Key];
9
9
  } & Pick<
10
- SecondType,
11
- RequiredKeysOf<SecondType> | Exclude<keyof SecondType, keyof FirstType>
10
+ SecondType,
11
+ RequiredKeysOf<SecondType> | Exclude<keyof SecondType, keyof FirstType>
12
12
  >;
13
13
 
14
14
  type TupleOrArray = readonly [...unknown[]];
@@ -76,8 +76,8 @@ baz(fooBar);
76
76
  @category Object
77
77
  */
78
78
  export type Spread<
79
- FirstType extends Spreadable,
80
- SecondType extends Spreadable,
79
+ FirstType extends Spreadable,
80
+ SecondType extends Spreadable,
81
81
  > = FirstType extends TupleOrArray
82
82
  ? SecondType extends TupleOrArray
83
83
  ? SpreadTupleOrArray<FirstType, SecondType>
@@ -7,7 +7,7 @@ Use-case: Changing interface values to strings in order to use them in a form mo
7
7
  ```
8
8
  import type {Stringified} from 'type-fest';
9
9
 
10
- type Car {
10
+ type Car = {
11
11
  model: string;
12
12
  speed: number;
13
13
  }
@@ -17,6 +17,8 @@ declare namespace TsConfigJson {
17
17
  | 'ES2020'
18
18
  | 'ES2022'
19
19
  | 'ESNext'
20
+ | 'Node16'
21
+ | 'NodeNext'
20
22
  | 'None'
21
23
  // Lowercase alternatives
22
24
  | 'commonjs'
@@ -28,6 +30,8 @@ declare namespace TsConfigJson {
28
30
  | 'es2020'
29
31
  | 'es2022'
30
32
  | 'esnext'
33
+ | 'node16'
34
+ | 'nodenext'
31
35
  | 'none';
32
36
 
33
37
  export type NewLine =
@@ -181,13 +185,13 @@ declare namespace TsConfigJson {
181
185
  | 'webworker.importscripts'
182
186
  | 'webworker.iterable';
183
187
 
184
- export interface Plugin {
188
+ export type Plugin = {
185
189
  [key: string]: unknown;
186
190
  /**
187
191
  Plugin name.
188
192
  */
189
193
  name?: string;
190
- }
194
+ };
191
195
 
192
196
  export type ImportsNotUsedAsValues =
193
197
  | 'remove'
@@ -219,7 +223,7 @@ declare namespace TsConfigJson {
219
223
 
220
224
  }
221
225
 
222
- export interface CompilerOptions {
226
+ export type CompilerOptions = {
223
227
  /**
224
228
  The character set of the input files.
225
229
 
@@ -993,7 +997,7 @@ declare namespace TsConfigJson {
993
997
  @default false
994
998
  */
995
999
  explainFiles?: boolean;
996
- }
1000
+ };
997
1001
 
998
1002
  namespace WatchOptions {
999
1003
  export type WatchFileKind =
@@ -1017,7 +1021,7 @@ declare namespace TsConfigJson {
1017
1021
  | 'FixedChunkSize';
1018
1022
  }
1019
1023
 
1020
- export interface WatchOptions {
1024
+ export type WatchOptions = {
1021
1025
 
1022
1026
  /**
1023
1027
  Specify the strategy for watching individual files.
@@ -1058,14 +1062,14 @@ declare namespace TsConfigJson {
1058
1062
  Specifies a list of files to exclude from watch
1059
1063
  */
1060
1064
  excludeFiles?: string[];
1061
- }
1065
+ };
1062
1066
 
1063
1067
  /**
1064
1068
  Auto type (.d.ts) acquisition options for this project.
1065
1069
 
1066
1070
  Requires TypeScript version 2.1 or later.
1067
1071
  */
1068
- export interface TypeAcquisition {
1072
+ export type TypeAcquisition = {
1069
1073
  /**
1070
1074
  Enable auto type acquisition.
1071
1075
  */
@@ -1080,9 +1084,9 @@ declare namespace TsConfigJson {
1080
1084
  Specifies a list of type declarations to be excluded from auto type acquisition. For example, `['jquery', 'lodash']`.
1081
1085
  */
1082
1086
  exclude?: string[];
1083
- }
1087
+ };
1084
1088
 
1085
- export interface References {
1089
+ export type References = {
1086
1090
  /**
1087
1091
  A normalized path on disk.
1088
1092
  */
@@ -1104,7 +1108,7 @@ declare namespace TsConfigJson {
1104
1108
  True if it is intended that this reference form a circularity.
1105
1109
  */
1106
1110
  circular?: boolean;
1107
- }
1111
+ };
1108
1112
  }
1109
1113
 
1110
1114
  /**
@@ -1112,7 +1116,7 @@ Type for [TypeScript's `tsconfig.json` file](https://www.typescriptlang.org/docs
1112
1116
 
1113
1117
  @category File
1114
1118
  */
1115
- export interface TsConfigJson {
1119
+ export type TsConfigJson = {
1116
1120
  /**
1117
1121
  Instructs the TypeScript compiler how to compile `.ts` files.
1118
1122
  */
@@ -1169,4 +1173,4 @@ export interface TsConfigJson {
1169
1173
  Requires TypeScript version 3.0 or later.
1170
1174
  */
1171
1175
  references?: TsConfigJson.References[];
1172
- }
1176
+ };
@@ -0,0 +1,51 @@
1
+ /**
2
+ Convert a tuple into a union type of its elements.
3
+
4
+ This can be useful when you have a fixed set of allowed values and want a type defining only the allowed values, but do not want to repeat yourself.
5
+
6
+ @example
7
+ ```
8
+ import type {TupleToUnion} from 'type-fest';
9
+
10
+ const destinations = ['a', 'b', 'c'] as const;
11
+
12
+ type Destination = TupleToUnion<typeof destinations>;
13
+ //=> 'a' | 'b' | 'c'
14
+
15
+ function verifyDestination(destination: unknown): destination is Destination {
16
+ return destinations.includes(destination as any);
17
+ }
18
+
19
+ type RequestBody = {
20
+ deliverTo: Destination;
21
+ };
22
+
23
+ function verifyRequestBody(body: unknown): body is RequestBody {
24
+ const deliverTo = (body as any).deliverTo;
25
+ return typeof body === 'object' && body !== null && verifyDestination(deliverTo);
26
+ }
27
+ ```
28
+
29
+ Alternatively, you may use `typeof destinations[number]`. If `destinations` is a tuple, there is no difference. However if `destinations` is a string, the resulting type will the union of the characters in the string. Other types of `destinations` may result in a compile error. In comparison, TupleToUnion will return `never` if a tuple is not provided.
30
+
31
+ @example
32
+ ```
33
+ const destinations = ['a', 'b', 'c'] as const;
34
+
35
+ type Destination = typeof destinations[number];
36
+ //=> 'a' | 'b' | 'c'
37
+
38
+ const erroringType = new Set(['a', 'b', 'c']);
39
+
40
+ type ErroringType = typeof erroringType[number];
41
+ //=> Type 'Set<string>' has no matching index signature for type 'number'. ts(2537)
42
+
43
+ const numberBool: { [n: number]: boolean } = { 1: true };
44
+
45
+ type NumberBool = typeof numberBool[number];
46
+ //=> boolean
47
+ ```
48
+
49
+ @category Array
50
+ */
51
+ export type TupleToUnion<ArrayType> = ArrayType extends readonly [infer Head, ...(infer Rest)] ? Head | TupleToUnion<Rest> : never;
@@ -55,6 +55,6 @@ export type UnionToIntersection<Union> = (
55
55
  : never
56
56
  // Infer the `Intersection` type since TypeScript represents the positional
57
57
  // arguments of unions of functions as an intersection of the union.
58
- ) extends ((mergedIntersection: infer Intersection) => void)
59
- ? Intersection
60
- : never;
58
+ ) extends ((mergedIntersection: infer Intersection) => void)
59
+ ? Intersection
60
+ : never;
@@ -33,8 +33,8 @@ type SomeWritable = Writable<Foo, 'b' | 'c'>;
33
33
  */
34
34
  export type Writable<BaseType, Keys extends keyof BaseType = keyof BaseType> =
35
35
  Simplify<
36
- // Pick just the keys that are not writable from the base type.
37
- Except<BaseType, Keys> &
38
- // Pick the keys that should be writable from the base type and make them writable by removing the `readonly` modifier from the key.
39
- {-readonly [KeyType in keyof Pick<BaseType, Keys>]: Pick<BaseType, Keys>[KeyType]}
36
+ // Pick just the keys that are not writable from the base type.
37
+ Except<BaseType, Keys> &
38
+ // Pick the keys that should be writable from the base type and make them writable by removing the `readonly` modifier from the key.
39
+ {-readonly [KeyType in keyof Pick<BaseType, Keys>]: Pick<BaseType, Keys>[KeyType]}
40
40
  >;
@@ -1,5 +0,0 @@
1
- import type {Writable} from './writable';
2
-
3
- /** @deprecated @see Writable */
4
- export type Mutable<BaseType, Keys extends keyof BaseType = keyof BaseType> =
5
- Writable<BaseType, Keys>;
@@ -1,29 +0,0 @@
1
- /**
2
- @deprecated Use the built-in [`Awaited` type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-5.html#the-awaited-type-and-promise-improvements) instead.
3
-
4
- Returns the type that is wrapped inside a `Promise` type.
5
- If the type is a nested Promise, it is unwrapped recursively until a non-Promise type is obtained.
6
- If the type is not a `Promise`, the type itself is returned.
7
-
8
- @example
9
- ```
10
- import type {PromiseValue} from 'type-fest';
11
-
12
- type AsyncData = Promise<string>;
13
- let asyncData: AsyncData = Promise.resolve('ABC');
14
-
15
- type Data = PromiseValue<AsyncData>;
16
- let data: Data = await asyncData;
17
-
18
- // Here's an example that shows how this type reacts to non-Promise types.
19
- type SyncData = PromiseValue<string>;
20
- let syncData: SyncData = getSyncData();
21
-
22
- // Here's an example that shows how this type reacts to recursive Promise types.
23
- type RecursiveAsyncData = Promise<Promise<string>>;
24
- let recursiveAsyncData: PromiseValue<RecursiveAsyncData> = await Promise.resolve(Promise.resolve('ABC'));
25
- ```
26
-
27
- @category Async
28
- */
29
- export type PromiseValue<PromiseType> = PromiseType extends PromiseLike<infer Value> ? PromiseValue<Value> : PromiseType;