type-fest 5.4.4 → 5.6.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 (100) hide show
  1. package/index.d.ts +10 -0
  2. package/package.json +6 -4
  3. package/readme.md +83 -58
  4. package/source/absolute.d.ts +52 -0
  5. package/source/all-extend.d.ts +5 -6
  6. package/source/all-union-fields.d.ts +18 -18
  7. package/source/and-all.d.ts +76 -0
  8. package/source/and.d.ts +4 -3
  9. package/source/array-length.d.ts +36 -0
  10. package/source/array-splice.d.ts +26 -26
  11. package/source/camel-case.d.ts +38 -5
  12. package/source/camel-cased-properties-deep.d.ts +11 -4
  13. package/source/camel-cased-properties.d.ts +5 -1
  14. package/source/conditional-pick-deep.d.ts +5 -3
  15. package/source/conditional-pick.d.ts +6 -4
  16. package/source/delimiter-case.d.ts +1 -0
  17. package/source/delimiter-cased-properties-deep.d.ts +8 -1
  18. package/source/delimiter-cased-properties.d.ts +5 -1
  19. package/source/empty-object.d.ts +1 -1
  20. package/source/entries.d.ts +1 -1
  21. package/source/entry.d.ts +1 -1
  22. package/source/exclude-exactly.d.ts +57 -0
  23. package/source/get.d.ts +1 -1
  24. package/source/greater-than-or-equal.d.ts +34 -1
  25. package/source/greater-than.d.ts +37 -3
  26. package/source/has-optional-keys.d.ts +1 -1
  27. package/source/has-readonly-keys.d.ts +1 -1
  28. package/source/has-required-keys.d.ts +1 -1
  29. package/source/has-writable-keys.d.ts +1 -1
  30. package/source/int-closed-range.d.ts +1 -3
  31. package/source/int-range.d.ts +3 -5
  32. package/source/internal/array.d.ts +7 -14
  33. package/source/internal/keys.d.ts +9 -9
  34. package/source/internal/numeric.d.ts +13 -23
  35. package/source/internal/tuple.d.ts +3 -3
  36. package/source/internal/type.d.ts +1 -0
  37. package/source/is-equal.d.ts +0 -1
  38. package/source/is-integer.d.ts +8 -8
  39. package/source/is-literal.d.ts +5 -5
  40. package/source/is-union.d.ts +12 -12
  41. package/source/iterable-element.d.ts +5 -5
  42. package/source/jsonify.d.ts +4 -4
  43. package/source/kebab-case.d.ts +1 -0
  44. package/source/kebab-cased-properties-deep.d.ts +7 -0
  45. package/source/kebab-cased-properties.d.ts +5 -1
  46. package/source/keys-of-union.d.ts +2 -2
  47. package/source/less-than-or-equal.d.ts +40 -4
  48. package/source/less-than.d.ts +35 -3
  49. package/source/literal-to-primitive.d.ts +1 -1
  50. package/source/literal-union.d.ts +1 -1
  51. package/source/merge-exclusive.d.ts +3 -3
  52. package/source/merge.d.ts +25 -0
  53. package/source/multidimensional-array.d.ts +1 -1
  54. package/source/multidimensional-readonly-array.d.ts +1 -1
  55. package/source/non-nullable-deep.d.ts +102 -0
  56. package/source/numeric.d.ts +3 -3
  57. package/source/omit-deep.d.ts +22 -23
  58. package/source/optional.d.ts +31 -0
  59. package/source/or-all.d.ts +73 -0
  60. package/source/or.d.ts +4 -11
  61. package/source/package-json.d.ts +7 -7
  62. package/source/partial-deep.d.ts +3 -1
  63. package/source/pascal-case.d.ts +1 -0
  64. package/source/pascal-cased-properties-deep.d.ts +7 -0
  65. package/source/pascal-cased-properties.d.ts +5 -1
  66. package/source/pick-deep.d.ts +8 -21
  67. package/source/readonly-deep.d.ts +5 -3
  68. package/source/remove-prefix.d.ts +15 -15
  69. package/source/replace.d.ts +2 -2
  70. package/source/require-all-or-none.d.ts +1 -1
  71. package/source/require-at-least-one.d.ts +5 -7
  72. package/source/require-exactly-one.d.ts +3 -3
  73. package/source/require-one-or-none.d.ts +1 -1
  74. package/source/required-deep.d.ts +3 -1
  75. package/source/screaming-snake-case.d.ts +1 -0
  76. package/source/set-non-nullable-deep.d.ts +6 -3
  77. package/source/set-non-nullable.d.ts +4 -11
  78. package/source/set-optional.d.ts +6 -10
  79. package/source/set-parameter-type.d.ts +2 -2
  80. package/source/set-readonly.d.ts +4 -8
  81. package/source/set-required-deep.d.ts +1 -1
  82. package/source/set-required.d.ts +4 -8
  83. package/source/shared-union-fields-deep.d.ts +1 -1
  84. package/source/shared-union-fields.d.ts +9 -9
  85. package/source/snake-case.d.ts +1 -0
  86. package/source/snake-cased-properties-deep.d.ts +7 -0
  87. package/source/snake-cased-properties.d.ts +5 -1
  88. package/source/some-extend.d.ts +113 -0
  89. package/source/spread.d.ts +1 -5
  90. package/source/subtract.d.ts +4 -3
  91. package/source/sum.d.ts +5 -4
  92. package/source/tagged.d.ts +5 -7
  93. package/source/tsconfig-json.d.ts +39 -7
  94. package/source/union-length.d.ts +27 -0
  95. package/source/union-member.d.ts +65 -0
  96. package/source/union-to-intersection.d.ts +1 -1
  97. package/source/union-to-tuple.d.ts +10 -19
  98. package/source/words.d.ts +30 -4
  99. package/source/writable.d.ts +15 -19
  100. package/source/xor.d.ts +1 -1
@@ -3,7 +3,7 @@ import type {HomomorphicPick} from './internal/index.d.ts';
3
3
  import type {Simplify} from './simplify.d.ts';
4
4
 
5
5
  /**
6
- Create a type that makes the given keys readonly. The remaining keys are kept as is.
6
+ Create a type that makes the given keys readonly, while keeping the remaining keys as is.
7
7
 
8
8
  Use-case: You want to define a single model where the only thing that changes is whether or not some of the keys are readonly.
9
9
 
@@ -18,11 +18,7 @@ type Foo = {
18
18
  };
19
19
 
20
20
  type SomeReadonly = SetReadonly<Foo, 'b' | 'c'>;
21
- // type SomeReadonly = {
22
- // a: number;
23
- // readonly b: string; // Was already readonly and still is.
24
- // readonly c: boolean; // Is now readonly.
25
- // }
21
+ //=> {a: number; readonly b: string; readonly c: boolean}
26
22
  ```
27
23
 
28
24
  @category Object
@@ -36,8 +32,8 @@ export type SetReadonly<BaseType, Keys extends keyof BaseType> =
36
32
  export type _SetReadonly<BaseType, Keys extends keyof BaseType> =
37
33
  BaseType extends unknown // To distribute `BaseType` when it's a union type.
38
34
  ? Simplify<
39
- Except<BaseType, Keys> &
40
- Readonly<HomomorphicPick<BaseType, Keys>>
35
+ Except<BaseType, Keys>
36
+ & Readonly<HomomorphicPick<BaseType, Keys>>
41
37
  >
42
38
  : never;
43
39
 
@@ -8,7 +8,7 @@ import type {RequiredDeep} from './required-deep.d.ts';
8
8
  import type {UnknownArray} from './unknown-array.d.ts';
9
9
 
10
10
  /**
11
- Create a type that makes the given keys required. You can specify deeply nested key paths. The remaining keys are kept as is.
11
+ Create a type that makes the given keys required, with support for deeply nested key paths, while keeping the remaining keys as is.
12
12
 
13
13
  Use-case: Selectively make nested properties required in complex types like models.
14
14
 
@@ -6,7 +6,7 @@ import type {Simplify} from './simplify.d.ts';
6
6
  import type {UnknownArray} from './unknown-array.d.ts';
7
7
 
8
8
  /**
9
- Create a type that makes the given keys required. The remaining keys are kept as is. The sister of the `SetOptional` type.
9
+ Create a type that makes the given keys required, while keeping the remaining keys as is.
10
10
 
11
11
  Use-case: You want to define a single model where the only thing that changes is whether or not some of the keys are required.
12
12
 
@@ -21,11 +21,7 @@ type Foo = {
21
21
  };
22
22
 
23
23
  type SomeRequired = SetRequired<Foo, 'b' | 'c'>;
24
- // type SomeRequired = {
25
- // a?: number;
26
- // b: string; // Was already required and still is.
27
- // c: boolean; // Is now required.
28
- // }
24
+ //=> {a?: number; b: string; c: boolean}
29
25
 
30
26
  // Set specific indices in an array to be required.
31
27
  type ArrayExample = SetRequired<[number?, number?, number?], 0 | 1>;
@@ -47,9 +43,9 @@ type _SetRequired<BaseType, Keys extends keyof BaseType> =
47
43
  : never
48
44
  : Simplify<
49
45
  // Pick just the keys that are optional from the base type.
50
- Except<BaseType, Keys> &
46
+ Except<BaseType, Keys>
51
47
  // Pick the keys that should be required from the base type and make them required.
52
- Required<HomomorphicPick<BaseType, Keys>>
48
+ & Required<HomomorphicPick<BaseType, Keys>>
53
49
  >;
54
50
 
55
51
  /**
@@ -89,7 +89,7 @@ function displayPetInfoWithSharedUnionFieldsDeep(petInfo: SharedUnionFieldsDeep<
89
89
  export type SharedUnionFieldsDeep<Union, Options extends SharedUnionFieldsDeepOptions = {}> =
90
90
  ApplyDefaultOptions<SharedUnionFieldsDeepOptions, DefaultSharedUnionFieldsDeepOptions, Options> extends infer OptionsWithDefaults extends Required<SharedUnionFieldsDeepOptions>
91
91
  // `Union extends` will convert `Union`
92
- // to a [distributive conditionaltype](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
92
+ // to a [distributive conditional type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
93
93
  // But this is not what we want, so we need to wrap `Union` with `[]` to prevent it.
94
94
  ? [Union] extends [NonRecursiveType | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown>]
95
95
  ? Union
@@ -66,14 +66,14 @@ function displayPetInfoWithSharedUnionFields(petInfo: SharedUnionFields<Cat | Do
66
66
  @category Union
67
67
  */
68
68
  export type SharedUnionFields<Union> =
69
- Extract<Union, NonRecursiveType | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown> | UnknownArray> extends infer SkippedMembers
70
- ? Exclude<Union, SkippedMembers> extends infer RelevantMembers
71
- ?
72
- | SkippedMembers
73
- | (IsNever<RelevantMembers> extends true
74
- ? never
75
- : Simplify<Pick<RelevantMembers, keyof RelevantMembers>>)
76
- : never
77
- : never;
69
+ Extract<Union, NonRecursiveType | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown> | UnknownArray> extends infer SkippedMembers
70
+ ? Exclude<Union, SkippedMembers> extends infer RelevantMembers
71
+ ? // eslint-disable-line @stylistic/operator-linebreak
72
+ | SkippedMembers
73
+ | (IsNever<RelevantMembers> extends true
74
+ ? never
75
+ : Simplify<Pick<RelevantMembers, keyof RelevantMembers>>)
76
+ : never
77
+ : never;
78
78
 
79
79
  export {};
@@ -16,6 +16,7 @@ import type {SnakeCase} from 'type-fest';
16
16
  const someVariable: SnakeCase<'fooBar'> = 'foo_bar';
17
17
  const noSplitOnNumbers: SnakeCase<'p2pNetwork'> = 'p2p_network';
18
18
  const splitOnNumbers: SnakeCase<'p2pNetwork', {splitOnNumbers: true}> = 'p_2_p_network';
19
+ const splitOnPunctuation: SnakeCase<'div.card::after', {splitOnPunctuation: true}> = 'div_card_after';
19
20
 
20
21
  // Advanced
21
22
 
@@ -51,6 +51,13 @@ const splitOnNumbers: SnakeCasedPropertiesDeep<{line1: {line2: [{line3: string}]
51
51
  ],
52
52
  },
53
53
  };
54
+
55
+ const splitOnPunctuation: SnakeCasedPropertiesDeep<{'user@info': {'user::id': number; 'user::name': string}}, {splitOnPunctuation: true}> = {
56
+ 'user_info': {
57
+ 'user_id': 1,
58
+ 'user_name': 'Tom',
59
+ },
60
+ };
54
61
  ```
55
62
 
56
63
  @category Change case
@@ -4,7 +4,7 @@ import type {ApplyDefaultOptions} from './internal/index.d.ts';
4
4
  import type {WordsOptions} from './words.d.ts';
5
5
 
6
6
  /**
7
- Convert object properties to snake case but not recursively.
7
+ Convert top-level object properties to snake case.
8
8
 
9
9
  This can be useful when, for example, converting some API types from a different style.
10
10
 
@@ -28,6 +28,10 @@ const result: SnakeCasedProperties<User> = {
28
28
  const splitOnNumbers: SnakeCasedProperties<{line1: string}, {splitOnNumbers: true}> = {
29
29
  'line_1': 'string',
30
30
  };
31
+
32
+ const splitOnPunctuation: SnakeCasedProperties<{'foo::bar': string}, {splitOnPunctuation: true}> = {
33
+ 'foo_bar': 'string',
34
+ };
31
35
  ```
32
36
 
33
37
  @category Change case
@@ -0,0 +1,113 @@
1
+ import type {CollapseRestElement} from './internal/array.d.ts';
2
+ import type {ApplyDefaultOptions} from './internal/object.d.ts';
3
+ import type {IfNotAnyOrNever, Not} from './internal/type.d.ts';
4
+ import type {IsAny} from './is-any.d.ts';
5
+ import type {IsNever} from './is-never.d.ts';
6
+ import type {Or} from './or.d.ts';
7
+ import type {UnknownArray} from './unknown-array.d.ts';
8
+
9
+ /**
10
+ @see {@link SomeExtend}
11
+ */
12
+ export type SomeExtendOptions = {
13
+ /**
14
+ Consider `never` elements to match the target type only if the target type itself is `never` (or `any`).
15
+
16
+ - When set to `true` (default), `never` is _not_ treated as a bottom type, instead, it is treated as a type that matches only itself (or `any`).
17
+ - When set to `false`, `never` is treated as a bottom type, and behaves as it normally would.
18
+
19
+ @default true
20
+
21
+ @example
22
+ ```
23
+ import type {SomeExtend} from 'type-fest';
24
+
25
+ type A = SomeExtend<[1, 2, never], string, {strictNever: true}>;
26
+ //=> false
27
+
28
+ type B = SomeExtend<[1, 2, never], string, {strictNever: false}>;
29
+ //=> true
30
+
31
+ type C = SomeExtend<[1, never], never, {strictNever: true}>;
32
+ //=> true
33
+
34
+ type D = SomeExtend<[1, never], never, {strictNever: false}>;
35
+ //=> true
36
+
37
+ type E = SomeExtend<[never], any, {strictNever: true}>;
38
+ //=> true
39
+
40
+ type F = SomeExtend<[never], any, {strictNever: false}>;
41
+ //=> true
42
+ ```
43
+ */
44
+ strictNever?: boolean;
45
+ };
46
+
47
+ type DefaultSomeExtendOptions = {
48
+ strictNever: true;
49
+ };
50
+
51
+ /**
52
+ Returns a boolean for whether some element in an array type extends another type.
53
+
54
+ @example
55
+ ```
56
+ import type {SomeExtend} from 'type-fest';
57
+
58
+ type A = SomeExtend<['1', '2', 3], number>;
59
+ //=> true
60
+
61
+ type B = SomeExtend<[1, 2, 3], string>;
62
+ //=> false
63
+
64
+ type C = SomeExtend<[string, number | string], number>;
65
+ //=> boolean
66
+
67
+ type D = SomeExtend<[true, boolean, true], false>;
68
+ //=> boolean
69
+ ```
70
+
71
+ Note: Behaviour of optional elements depend on the `exactOptionalPropertyTypes` compiler option. When the option is disabled, the target type must include `undefined` for a successful match.
72
+
73
+ ```
74
+ // @exactOptionalPropertyTypes: true
75
+ import type {SomeExtend} from 'type-fest';
76
+
77
+ type A = SomeExtend<[1?, 2?, '3'?], string>;
78
+ //=> true
79
+ ```
80
+
81
+ ```
82
+ // @exactOptionalPropertyTypes: false
83
+ import type {SomeExtend} from 'type-fest';
84
+
85
+ type A = SomeExtend<[1?, 2?, '3'?], string>;
86
+ //=> boolean
87
+
88
+ type B = SomeExtend<[1?, 2?, '3'?], string | undefined>;
89
+ //=> true
90
+ ```
91
+
92
+ @see {@link SomeExtendOptions}
93
+
94
+ @category Utilities
95
+ @category Array
96
+ */
97
+ export type SomeExtend<TArray extends UnknownArray, Type, Options extends SomeExtendOptions = {}> =
98
+ _SomeExtend<CollapseRestElement<TArray>, Type, ApplyDefaultOptions<SomeExtendOptions, DefaultSomeExtendOptions, Options>>;
99
+
100
+ type _SomeExtend<TArray extends UnknownArray, Type, Options extends Required<SomeExtendOptions>> = IfNotAnyOrNever<TArray,
101
+ TArray extends readonly [infer First, ...infer Rest]
102
+ ? IsNever<First> extends true
103
+ ? Or<Or<IsNever<Type>, IsAny<Type>>, Not<Options['strictNever']>> extends true
104
+ // If target `Type` is also `never`, or is `any`, or `strictNever` is disabled, return `true`.
105
+ ? true
106
+ : _SomeExtend<Rest, Type, Options>
107
+ : First extends Type
108
+ ? true
109
+ : _SomeExtend<Rest, Type, Options>
110
+ : false,
111
+ false, false>;
112
+
113
+ export {};
@@ -41,11 +41,7 @@ const bar = {c: false};
41
41
  const fooBar = {...foo, ...bar};
42
42
 
43
43
  type FooBar = Spread<Foo, Bar>;
44
- // type FooBar = {
45
- // a: number;
46
- // b?: string | number | undefined;
47
- // c: boolean;
48
- // }
44
+ //=> {a: number; b?: string | number; c: boolean}
49
45
 
50
46
  declare function baz(argument: FooBar): void;
51
47
 
@@ -1,7 +1,8 @@
1
- import type {NumberAbsolute, ReverseSign} from './internal/index.d.ts';
1
+ import type {ReverseSign} from './internal/index.d.ts';
2
2
  import type {PositiveInfinity, NegativeInfinity, IsNegative} from './numeric.d.ts';
3
3
  import type {LessThan} from './less-than.d.ts';
4
4
  import type {TupleOf} from './tuple-of.d.ts';
5
+ import type {Absolute} from './absolute.d.ts';
5
6
 
6
7
  /**
7
8
  Returns the difference between two numbers.
@@ -59,9 +60,9 @@ type SubtractPostChecks<A extends number, B extends number, AreNegative = [IsNeg
59
60
  ? SubtractPositives<A, B>
60
61
  : AreNegative extends [true, true]
61
62
  // When both numbers are negative we subtract the absolute values and then reverse the sign
62
- ? ReverseSign<SubtractPositives<NumberAbsolute<A>, NumberAbsolute<B>>>
63
+ ? ReverseSign<SubtractPositives<Absolute<A>, Absolute<B>>>
63
64
  // When the signs are different we can add the absolute values and then reverse the sign if A < B
64
- : [...TupleOf<NumberAbsolute<A>>, ...TupleOf<NumberAbsolute<B>>] extends infer R extends unknown[]
65
+ : [...TupleOf<Absolute<A>>, ...TupleOf<Absolute<B>>] extends infer R extends unknown[]
65
66
  ? LessThan<A, B> extends true ? ReverseSign<R['length']> : R['length']
66
67
  : never;
67
68
 
package/source/sum.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import type {TupleOf} from './tuple-of.d.ts';
2
- import type {NumberAbsolute, TupleMax, ReverseSign} from './internal/index.d.ts';
2
+ import type {TupleMax, ReverseSign} from './internal/index.d.ts';
3
3
  import type {PositiveInfinity, NegativeInfinity, IsNegative} from './numeric.d.ts';
4
4
  import type {Subtract} from './subtract.d.ts';
5
+ import type {Absolute} from './absolute.d.ts';
5
6
 
6
7
  /**
7
8
  Returns the sum of two numbers.
@@ -57,11 +58,11 @@ type SumPostChecks<A extends number, B extends number, AreNegative = [IsNegative
57
58
  ? SumPositives<A, B>
58
59
  : AreNegative extends [true, true]
59
60
  // When both numbers are negative we add the absolute values and then reverse the sign
60
- ? ReverseSign<SumPositives<NumberAbsolute<A>, NumberAbsolute<B>>>
61
+ ? ReverseSign<SumPositives<Absolute<A>, Absolute<B>>>
61
62
  // When the signs are different we can subtract the absolute values, remove the sign
62
63
  // and then reverse the sign if the larger absolute value is negative
63
- : NumberAbsolute<Subtract<NumberAbsolute<A>, NumberAbsolute<B>>> extends infer Result extends number
64
- ? TupleMax<[NumberAbsolute<A>, NumberAbsolute<B>]> extends infer Max_ extends number
64
+ : Absolute<Subtract<Absolute<A>, Absolute<B>>> extends infer Result extends number
65
+ ? TupleMax<[Absolute<A>, Absolute<B>]> extends infer Max_ extends number
65
66
  ? Max_ extends A | B
66
67
  // The larger absolute value is positive, so the result is positive
67
68
  ? Result
@@ -8,12 +8,10 @@ export type TagContainer<Token> = {
8
8
  type Tag<Token extends PropertyKey, TagMetadata> = TagContainer<{[K in Token]: TagMetadata}>;
9
9
 
10
10
  /**
11
- Attach a "tag" to an arbitrary type. This allows you to create distinct types, that aren't assignable to one another, for distinct concepts in your program that should not be interchangeable, even if their runtime values have the same type. (See examples.)
11
+ Create a [tagged type](https://medium.com/@KevinBGreene/surviving-the-typescript-ecosystem-branding-and-type-tagging-6cf6e516523d) that can support [multiple tags](https://github.com/sindresorhus/type-fest/issues/665) and [per-tag metadata](https://medium.com/@ethanresnick/advanced-typescript-tagged-types-improved-with-type-level-metadata-5072fc125fcf).
12
12
 
13
13
  A type returned by `Tagged` can be passed to `Tagged` again, to create a type with multiple tags.
14
14
 
15
- [Read more about tagged types.](https://medium.com/@KevinBGreene/surviving-the-typescript-ecosystem-branding-and-type-tagging-6cf6e516523d)
16
-
17
15
  A tag's name is usually a string (and must be a string, number, or symbol), but each application of a tag can also contain an arbitrary type as its "metadata". See {@link GetTagMetadata} for examples and explanation.
18
16
 
19
17
  A type `A` returned by `Tagged` is assignable to another type `B` returned by `Tagged` if and only if:
@@ -100,7 +98,7 @@ const parsed = parse(x); // The type of `parsed` is { hello: string }
100
98
  export type GetTagMetadata<Type extends Tag<TagName, unknown>, TagName extends PropertyKey> = Type[typeof tag][TagName];
101
99
 
102
100
  /**
103
- Revert a tagged type back to its original type by removing all tags.
101
+ Get the untagged portion of a tagged type created with `Tagged`.
104
102
 
105
103
  Why is this necessary?
106
104
 
@@ -121,7 +119,7 @@ const moneyByAccountType: Record<UnwrapTagged<AccountType>, number> = {
121
119
  // Without UnwrapTagged, the following expression would throw a type error.
122
120
  const money = moneyByAccountType.SAVINGS; // TS error: Property 'SAVINGS' does not exist
123
121
 
124
- // Attempting to pass an non-Tagged type to UnwrapTagged will raise a type error.
122
+ // Attempting to pass a non-Tagged type to UnwrapTagged will raise a type error.
125
123
  // @ts-expect-error
126
124
  type WontWork = UnwrapTagged<string>;
127
125
  ```
@@ -129,7 +127,7 @@ type WontWork = UnwrapTagged<string>;
129
127
  @category Type
130
128
  */
131
129
  export type UnwrapTagged<TaggedType extends Tag<PropertyKey, any>> =
132
- RemoveAllTags<TaggedType>;
130
+ RemoveAllTags<TaggedType>;
133
131
 
134
132
  type RemoveAllTags<T> = T extends Tag<PropertyKey, any>
135
133
  ? {
@@ -239,7 +237,7 @@ const moneyByAccountType: Record<UnwrapOpaque<AccountType>, number> = {
239
237
  // Without UnwrapOpaque, the following expression would throw a type error.
240
238
  const money = moneyByAccountType.SAVINGS; // TS error: Property 'SAVINGS' does not exist
241
239
 
242
- // Attempting to pass an non-Opaque type to UnwrapOpaque will raise a type error.
240
+ // Attempting to pass a non-Opaque type to UnwrapOpaque will raise a type error.
243
241
  // @ts-expect-error
244
242
  type WontWork = UnwrapOpaque<string>;
245
243
 
@@ -61,6 +61,7 @@ export namespace TsConfigJson {
61
61
  | 'ES2022'
62
62
  | 'ES2023'
63
63
  | 'ES2024'
64
+ | 'ES2025'
64
65
  | 'ESNext'
65
66
  // Lowercase alternatives
66
67
  | 'es3'
@@ -76,6 +77,7 @@ export namespace TsConfigJson {
76
77
  | 'es2022'
77
78
  | 'es2023'
78
79
  | 'es2024'
80
+ | 'es2025'
79
81
  | 'esnext';
80
82
 
81
83
  type Lib =
@@ -148,6 +150,13 @@ export namespace TsConfigJson {
148
150
  | 'ES2024.Regexp'
149
151
  | 'ES2024.SharedMemory'
150
152
  | 'ES2024.String'
153
+ | 'ES2025'
154
+ | 'ES2025.Collection'
155
+ | 'ES2025.Float16'
156
+ | 'ES2025.Intl'
157
+ | 'ES2025.Iterator'
158
+ | 'ES2025.Promise'
159
+ | 'ES2025.RegExp'
151
160
  | 'ESNext'
152
161
  | 'ESNext.Array'
153
162
  | 'ESNext.AsyncIterable'
@@ -156,6 +165,7 @@ export namespace TsConfigJson {
156
165
  | 'ESNext.Decorators'
157
166
  | 'ESNext.Disposable'
158
167
  | 'ESNext.Error'
168
+ | 'ESNext.Float16'
159
169
  | 'ESNext.Intl'
160
170
  | 'ESNext.Iterator'
161
171
  | 'ESNext.Object'
@@ -163,6 +173,7 @@ export namespace TsConfigJson {
163
173
  | 'ESNext.Regexp'
164
174
  | 'ESNext.String'
165
175
  | 'ESNext.Symbol'
176
+ | 'ESNext.Temporal'
166
177
  | 'ESNext.WeakRef'
167
178
  | 'DOM'
168
179
  | 'DOM.AsyncIterable'
@@ -244,6 +255,13 @@ export namespace TsConfigJson {
244
255
  | 'es2024.regexp'
245
256
  | 'es2024.sharedmemory'
246
257
  | 'es2024.string'
258
+ | 'es2025'
259
+ | 'es2025.collection'
260
+ | 'es2025.float16'
261
+ | 'es2025.intl'
262
+ | 'es2025.iterator'
263
+ | 'es2025.promise'
264
+ | 'es2025.regexp'
247
265
  | 'esnext'
248
266
  | 'esnext.array'
249
267
  | 'esnext.asynciterable'
@@ -252,6 +270,7 @@ export namespace TsConfigJson {
252
270
  | 'esnext.decorators'
253
271
  | 'esnext.disposable'
254
272
  | 'esnext.error'
273
+ | 'esnext.float16'
255
274
  | 'esnext.intl'
256
275
  | 'esnext.iterator'
257
276
  | 'esnext.object'
@@ -259,6 +278,7 @@ export namespace TsConfigJson {
259
278
  | 'esnext.regexp'
260
279
  | 'esnext.string'
261
280
  | 'esnext.symbol'
281
+ | 'esnext.temporal'
262
282
  | 'esnext.weakref'
263
283
  | 'dom'
264
284
  | 'dom.asynciterable'
@@ -326,7 +346,7 @@ export namespace TsConfigJson {
326
346
  | 'legacy'
327
347
  | 'force';
328
348
 
329
- type IgnoreDeprecations = '5.0';
349
+ type IgnoreDeprecations = '5.0' | '6.0';
330
350
  }
331
351
 
332
352
  type CompilerOptions = {
@@ -479,7 +499,7 @@ export namespace TsConfigJson {
479
499
  /**
480
500
  Specify module code generation: 'None', 'CommonJS', 'AMD', 'System', 'UMD', 'ES6', 'ES2015' or 'ESNext'. Only 'AMD' and 'System' can be used in conjunction with `--outFile`. 'ES6' and 'ES2015' values may be used when targeting 'ES5' or lower.
481
501
 
482
- @default ['ES3', 'ES5'].includes(target) ? 'CommonJS' : 'ES6'
502
+ Default: `'ESNext'` since TypeScript 6.0, `['ES3', 'ES5'].includes(target) ? 'CommonJS' : 'ES6'` before.
483
503
  */
484
504
  module?: CompilerOptions.Module;
485
505
 
@@ -587,8 +607,17 @@ export namespace TsConfigJson {
587
607
  */
588
608
  skipLibCheck?: boolean;
589
609
 
610
+ /**
611
+ Enforce stable type ordering.
612
+
613
+ @default false
614
+ */
615
+ stableTypeOrdering?: boolean;
616
+
590
617
  /**
591
618
  Concatenate and emit output to single file.
619
+
620
+ @deprecated since TypeScript 6.0.
592
621
  */
593
622
  outFile?: string;
594
623
 
@@ -696,7 +725,7 @@ export namespace TsConfigJson {
696
725
  /**
697
726
  Specify ECMAScript target version.
698
727
 
699
- @default 'es3'
728
+ Default: Current-year ES version since TypeScript 6.0, `'es3'` before.
700
729
  */
701
730
  target?: CompilerOptions.Target;
702
731
 
@@ -776,7 +805,7 @@ export namespace TsConfigJson {
776
805
  /**
777
806
  Report error if failed to find a source file for a side effect import.
778
807
 
779
- @default false
808
+ Default: `true` since TypeScript 6.0, `false` before.
780
809
  */
781
810
  noUncheckedSideEffectImports?: boolean;
782
811
 
@@ -822,6 +851,8 @@ export namespace TsConfigJson {
822
851
 
823
852
  /**
824
853
  Base directory to resolve non-relative module names.
854
+
855
+ @deprecated since TypeScript 6.0.
825
856
  */
826
857
  baseUrl?: string;
827
858
 
@@ -944,7 +975,7 @@ export namespace TsConfigJson {
944
975
  /**
945
976
  Enable all strict type checking options.
946
977
 
947
- @default false
978
+ Default: `true` since TypeScript 6.0, `false` before.
948
979
  */
949
980
  strict?: boolean;
950
981
 
@@ -959,6 +990,7 @@ export namespace TsConfigJson {
959
990
  Provide full support for iterables in `for-of`, spread, and destructuring when targeting `ES5` or `ES3`.
960
991
 
961
992
  @default false
993
+ @deprecated since TypeScript 6.0.
962
994
  */
963
995
  downlevelIteration?: boolean;
964
996
 
@@ -1150,7 +1182,7 @@ export namespace TsConfigJson {
1150
1182
  /**
1151
1183
  Enable lib replacement.
1152
1184
 
1153
- @default true
1185
+ Default: `false` since TypeScript 6.0, `true` before.
1154
1186
  */
1155
1187
  libReplacement?: boolean;
1156
1188
  };
@@ -1266,7 +1298,7 @@ export namespace TsConfigJson {
1266
1298
  }
1267
1299
 
1268
1300
  /**
1269
- Type for [TypeScript's `tsconfig.json` file](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) (TypeScript 3.7).
1301
+ Type for [TypeScript's `tsconfig.json` file](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html).
1270
1302
 
1271
1303
  @category File
1272
1304
  */
@@ -0,0 +1,27 @@
1
+ import type {UnionToTuple} from './union-to-tuple.d.ts';
2
+
3
+ /**
4
+ Returns the length of a union type.
5
+
6
+ @example
7
+ ```
8
+ import type {UnionLength} from 'type-fest';
9
+
10
+ type T1 = UnionLength<'foo' | 'bar' | 'baz'>;
11
+ //=> 3
12
+
13
+ type T2 = UnionLength<[string, string, string] | {x: string; y: string; z: string}>;
14
+ //=> 2
15
+
16
+ type T3 = UnionLength<any>;
17
+ //=> 1
18
+
19
+ type T4 = UnionLength<never>;
20
+ //=> 0
21
+ ```
22
+
23
+ @category Type
24
+ */
25
+ export type UnionLength<Union> = UnionToTuple<Union>['length'];
26
+
27
+ export {};
@@ -0,0 +1,65 @@
1
+ import type {UnionToIntersection} from './union-to-intersection.d.ts';
2
+ import type {IsNever} from './is-never.d.ts';
3
+
4
+ /**
5
+ Returns an arbitrary member of a union type.
6
+
7
+ Use-cases:
8
+ - Implementing recursive type functions that accept a union type.
9
+
10
+ @example
11
+ ```
12
+ import type {UnionMember, IsNever} from 'type-fest';
13
+
14
+ type UnionLength<T, Acc extends any[] = []> =
15
+ UnionMember<T> extends infer Member
16
+ ? IsNever<Member> extends false
17
+ ? UnionLength<Exclude<T, Member>, [...Acc, Member]>
18
+ : Acc['length']
19
+ : never;
20
+
21
+ type T1 = UnionLength<'foo' | 'bar' | 'baz'>;
22
+ //=> 3
23
+
24
+ type T2 = UnionLength<{a: string}>;
25
+ //=> 1
26
+ ```
27
+
28
+ - Picking an arbitrary member from a union
29
+
30
+ @example
31
+ ```
32
+ import type {UnionMember, Primitive, LiteralToPrimitive} from 'type-fest';
33
+
34
+ type IsHomogenous<T extends Primitive> = [T] extends [LiteralToPrimitive<UnionMember<T>>] ? true : false;
35
+
36
+ type T1 = IsHomogenous<1 | 2 | 3 | 4>;
37
+ //=> true
38
+
39
+ type T2 = IsHomogenous<'foo' | 'bar'>;
40
+ //=> true
41
+
42
+ type T3 = IsHomogenous<'foo' | 'bar' | 1>;
43
+ //=> false
44
+ ```
45
+
46
+ Returns `never` when the input is `never`.
47
+
48
+ @example
49
+ ```
50
+ import type {UnionMember} from 'type-fest';
51
+
52
+ type LastNever = UnionMember<never>;
53
+ //=> never
54
+ ```
55
+
56
+ @category Type
57
+ */
58
+ export type UnionMember<T> =
59
+ IsNever<T> extends true
60
+ ? never
61
+ : UnionToIntersection<T extends any ? () => T : never> extends () => (infer R)
62
+ ? R
63
+ : never;
64
+
65
+ export {};
@@ -1,5 +1,5 @@
1
1
  /**
2
- Convert a union type to an intersection type using [distributive conditional types](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
2
+ Convert a union type to an intersection type.
3
3
 
4
4
  Inspired by [this Stack Overflow answer](https://stackoverflow.com/a/50375286/2172153).
5
5