type-fest 4.16.0 → 4.18.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-fest",
3
- "version": "4.16.0",
3
+ "version": "4.18.0",
4
4
  "description": "A collection of essential TypeScript types",
5
5
  "license": "(MIT OR CC0-1.0)",
6
6
  "repository": "sindresorhus/type-fest",
@@ -39,10 +39,10 @@
39
39
  "generics"
40
40
  ],
41
41
  "devDependencies": {
42
- "expect-type": "^0.15.0",
42
+ "expect-type": "^0.19.0",
43
43
  "npm-run-all2": "^6.1.2",
44
44
  "tsd": "^0.31.0",
45
- "typescript": "~5.4.3",
45
+ "typescript": "~5.4.5",
46
46
  "xo": "^0.58.0"
47
47
  },
48
48
  "xo": {
package/readme.md CHANGED
@@ -151,10 +151,8 @@ Click the type names for complete docs.
151
151
  - [`UndefinedOnPartialDeep`](source/undefined-on-partial-deep.d.ts) - Create a deep version of another type where all optional keys are set to also accept `undefined`.
152
152
  - [`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.
153
153
  - [`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).
154
- - [`Tagged`](source/opaque.d.ts) - 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) if needed.
155
- - [`UnwrapTagged`](source/opaque.d.ts) - Get the untagged portion of a tagged type created with `Tagged`.
156
- - [`Opaque`](source/opaque.d.ts) - Create a [tagged type](https://medium.com/@KevinBGreene/surviving-the-typescript-ecosystem-branding-and-type-tagging-6cf6e516523d). This implementation only supports a single tag.
157
- - [`UnwrapOpaque`](source/opaque.d.ts) - Get the untagged portion of a tagged type created with `Opaque` or `Tagged`.
154
+ - [`Tagged`](source/opaque.d.ts) - 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). (This replaces the previous [`Opaque`](source/opaque.d.ts) type, which is now deprecated.)
155
+ - [`UnwrapTagged`](source/opaque.d.ts) - Get the untagged portion of a tagged type created with `Tagged`. (This replaces the previous [`UnwrapOpaque`](source/opaque.d.ts) type, which is now deprecated.)
158
156
  - [`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.
159
157
  - [`SetOptional`](source/set-optional.d.ts) - Create a type that makes the given keys optional.
160
158
  - [`SetReadonly`](source/set-readonly.d.ts) - Create a type that makes the given keys readonly.
@@ -200,6 +198,8 @@ Click the type names for complete docs.
200
198
  - [`SharedUnionFieldsDeep`](source/shared-union-fields-deep.d.ts) - Create a type with shared fields from a union of object types, deeply traversing nested structures.
201
199
  - [`DistributedOmit`](source/distributed-omit.d.ts) - Omits keys from a type, distributing the operation over a union.
202
200
  - [`DistributedPick`](source/distributed-pick.d.ts) - Picks keys from a type, distributing the operation over a union.
201
+ - [`And`](source/and.d.ts) - Returns a boolean for whether two given types are both true.
202
+ - [`Or`](source/or.d.ts) - Returns a boolean for whether either of two given types are true.
203
203
 
204
204
  ### Type Guard
205
205
 
@@ -233,10 +233,11 @@ type ShouldBeNever = IfAny<'not any', 'not never', 'never'>;
233
233
  - [`IsNumericLiteral`](source/is-literal.d.ts) - Returns a boolean for whether the given type is a `number` or `bigint` [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types).
234
234
  - [`IsBooleanLiteral`](source/is-literal.d.ts) - Returns a boolean for whether the given type is a `true` or `false` [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types).
235
235
  - [`IsSymbolLiteral`](source/is-literal.d.ts) - Returns a boolean for whether the given type is a `symbol` [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types).
236
- - [`IsAny`](source/is-any.d.ts) - Returns a boolean for whether the given type is `any`. (Conditional version: [`IfAny`](source/if-any.d.ts).)
237
- - [`IsNever`](source/is-never.d.ts) - Returns a boolean for whether the given type is `never`. (Conditional version: [`IfNever`](source/if-never.d.ts).)
238
- - [`IsUnknown`](source/is-unknown.d.ts) - Returns a boolean for whether the given type is `unknown`. (Conditional version: [`IfUnknown`](source/if-unknown.d.ts).)
236
+ - [`IsAny`](source/is-any.d.ts) - Returns a boolean for whether the given type is `any`. (Conditional version: [`IfAny`](source/if-any.d.ts))
237
+ - [`IsNever`](source/is-never.d.ts) - Returns a boolean for whether the given type is `never`. (Conditional version: [`IfNever`](source/if-never.d.ts))
238
+ - [`IsUnknown`](source/is-unknown.d.ts) - Returns a boolean for whether the given type is `unknown`. (Conditional version: [`IfUnknown`](source/if-unknown.d.ts))
239
239
  - [`IsEmptyObject`](source/empty-object.d.ts) - Returns a boolean for whether the type is strictly equal to an empty plain object, the `{}` value. (Conditional version: [`IfEmptyObject`](source/if-empty-object.d.ts))
240
+ - [`IsNull`](source/is-null.d.ts) - Returns a boolean for whether the given type is `null`. (Conditional version: [`IfNull`](source/if-null.d.ts))
240
241
 
241
242
  ### JSON
242
243
 
@@ -344,7 +345,8 @@ type ShouldBeNever = IfAny<'not any', 'not never', 'never'>;
344
345
  - `RequireOnlyOne` - See [`RequireExactlyOne`](source/require-exactly-one.d.ts)
345
346
  - `AtMostOne` - See [`RequireOneOrNone`](source/require-one-or-none.d.ts)
346
347
  - `AllKeys` - See [`KeysOfUnion`](source/keys-of-union.d.ts)
347
- - `Branded` - See [`Opaque`](source/opaque.d.ts)
348
+ - `Branded` - See [`Tagged`](source/opaque.d.ts)
349
+ - `Opaque` - See [`Tagged`](source/opaque.d.ts)
348
350
 
349
351
  ## Tips
350
352
 
@@ -0,0 +1,25 @@
1
+ import type {IsEqual} from './is-equal';
2
+
3
+ /**
4
+ Returns a boolean for whether two given types are both true.
5
+
6
+ Use-case: Constructing complex conditional types where multiple conditions must be satisfied.
7
+
8
+ @example
9
+ ```
10
+ import type {And} from 'type-fest';
11
+
12
+ And<true, true>;
13
+ //=> true
14
+
15
+ And<true, false>;
16
+ //=> false
17
+ ```
18
+
19
+ @see {@link Or}
20
+ */
21
+ export type And<A extends boolean, B extends boolean> = [A, B][number] extends true
22
+ ? true
23
+ : true extends [IsEqual<A, false>, IsEqual<B, false>][number]
24
+ ? false
25
+ : never;
@@ -3,8 +3,9 @@ import type {LessThanOrEqual} from './less-than-or-equal';
3
3
  import type {GreaterThanOrEqual} from './greater-than-or-equal';
4
4
  import type {GreaterThan} from './greater-than';
5
5
  import type {IsNegative} from './numeric';
6
- import type {And, Not, ArrayMin} from './internal';
6
+ import type {Not, ArrayMin} from './internal';
7
7
  import type {IsEqual} from './is-equal';
8
+ import type {And} from './and';
8
9
  import type {ArraySplice} from './array-splice';
9
10
 
10
11
  /**
@@ -1,6 +1,8 @@
1
- import type {NumberAbsolute, And, Or, PositiveNumericStringGt} from './internal';
1
+ import type {NumberAbsolute, PositiveNumericStringGt} from './internal';
2
2
  import type {IsEqual} from './is-equal';
3
3
  import type {PositiveInfinity, NegativeInfinity, IsNegative} from './numeric';
4
+ import type {And} from './and';
5
+ import type {Or} from './or';
4
6
 
5
7
  /**
6
8
  Returns a boolean for whether a given number is greater than another number.
@@ -0,0 +1,24 @@
1
+ import type {IsNull} from './is-null';
2
+
3
+ /**
4
+ An if-else-like type that resolves depending on whether the given type is `null`.
5
+
6
+ @see {@link IsNull}
7
+
8
+ @example
9
+ ```
10
+ import type {IfNull} from 'type-fest';
11
+
12
+ type ShouldBeTrue = IfNull<null>;
13
+ //=> true
14
+
15
+ type ShouldBeBar = IfNull<'not null', 'foo', 'bar'>;
16
+ //=> 'bar'
17
+ ```
18
+
19
+ @category Type Guard
20
+ @category Utilities
21
+ */
22
+ export type IfNull<T, TypeIfNull = true, TypeIfNotNull = false> = (
23
+ IsNull<T> extends true ? TypeIfNull : TypeIfNotNull
24
+ );
@@ -429,11 +429,6 @@ Returns a boolean for whether the given `boolean` is not `false`.
429
429
  */
430
430
  export type IsNotFalse<T extends boolean> = [T] extends [false] ? false : true;
431
431
 
432
- /**
433
- Returns a boolean for whether the given type is `null`.
434
- */
435
- export type IsNull<T> = [T] extends [null] ? true : false;
436
-
437
432
  /**
438
433
  Disallows any of the given keys.
439
434
  */
@@ -456,42 +451,6 @@ IsPrimitive<Object>
456
451
  */
457
452
  export type IsPrimitive<T> = [T] extends [Primitive] ? true : false;
458
453
 
459
- /**
460
- Returns a boolean for whether A and B are both true.
461
-
462
- @example
463
- ```
464
- And<true, true>;
465
- //=> true
466
-
467
- And<true, false>;
468
- //=> false
469
- ```
470
- */
471
- export type And<A extends boolean, B extends boolean> = [A, B][number] extends true
472
- ? true
473
- : true extends [IsEqual<A, false>, IsEqual<B, false>][number]
474
- ? false
475
- : never;
476
-
477
- /**
478
- Returns a boolean for either A or B is true.
479
-
480
- @example
481
- ```
482
- Or<true, false>;
483
- //=> true
484
-
485
- Or<false, false>;
486
- //=> false
487
- ```
488
- */
489
- export type Or<A extends boolean, B extends boolean> = [A, B][number] extends false
490
- ? false
491
- : true extends [IsEqual<A, true>, IsEqual<B, true>][number]
492
- ? true
493
- : never;
494
-
495
454
  /**
496
455
  Returns a boolean for whether A is false.
497
456
 
@@ -9,14 +9,7 @@ Useful in type utilities, such as checking if something does not occur.
9
9
 
10
10
  @example
11
11
  ```
12
- import type {IsNever} from 'type-fest';
13
-
14
- type And<A, B> =
15
- A extends true
16
- ? B extends true
17
- ? true
18
- : false
19
- : false;
12
+ import type {IsNever, And} from 'type-fest';
20
13
 
21
14
  // https://github.com/andnp/SimplyTyped/blob/master/src/types/strings.ts
22
15
  type AreStringsEqual<A extends string, B extends string> =
@@ -0,0 +1,20 @@
1
+ /**
2
+ Returns a boolean for whether the given type is `null`.
3
+
4
+ @example
5
+ ```
6
+ import type {IsNull} from 'type-fest';
7
+
8
+ type NonNullFallback<T, Fallback> = IsNull<T> extends true ? Fallback : T;
9
+
10
+ type Example1 = NonNullFallback<null, string>;
11
+ //=> string
12
+
13
+ type Example2 = NonNullFallback<number, string>;
14
+ //=? number
15
+ ```
16
+
17
+ @category Type Guard
18
+ @category Utilities
19
+ */
20
+ export type IsNull<T> = [T] extends [null] ? true : false;
@@ -1,4 +1,4 @@
1
- import type {IsNull} from './internal';
1
+ import type {IsNull} from './is-null';
2
2
 
3
3
  /**
4
4
  Returns a boolean for whether the given type is `unknown`.
@@ -3,7 +3,7 @@ import type {IsInteger} from './is-integer';
3
3
 
4
4
  export type Numeric = number | bigint;
5
5
 
6
- type Zero = 0 | 0n;
6
+ export type Zero = 0 | 0n;
7
7
 
8
8
  /**
9
9
  Matches the hidden `Infinity` type.
@@ -76,6 +76,7 @@ type Person = {
76
76
  ```
77
77
 
78
78
  @category Type
79
+ @deprecated Use {@link Tagged} instead
79
80
  */
80
81
  export type Opaque<Type, Token = unknown> = Type & TagContainer<Token>;
81
82
 
@@ -109,6 +110,7 @@ type WillWork = UnwrapOpaque<Tagged<number, 'AccountNumber'>>; // number
109
110
  ```
110
111
 
111
112
  @category Type
113
+ @deprecated Use {@link UnwrapTagged} instead
112
114
  */
113
115
  export type UnwrapOpaque<OpaqueType extends TagContainer<unknown>> =
114
116
  OpaqueType extends Tag<PropertyKey, any>
package/source/or.d.ts ADDED
@@ -0,0 +1,25 @@
1
+ import type {IsEqual} from './is-equal';
2
+
3
+ /**
4
+ Returns a boolean for whether either of two given types are true.
5
+
6
+ Use-case: Constructing complex conditional types where multiple conditions must be satisfied.
7
+
8
+ @example
9
+ ```
10
+ import type {Or} from 'type-fest';
11
+
12
+ Or<true, false>;
13
+ //=> true
14
+
15
+ Or<false, false>;
16
+ //=> false
17
+ ```
18
+
19
+ @see {@link And}
20
+ */
21
+ export type Or<A extends boolean, B extends boolean> = [A, B][number] extends false
22
+ ? false
23
+ : true extends [IsEqual<A, true>, IsEqual<B, true>][number]
24
+ ? true
25
+ : never;
@@ -1,8 +1,10 @@
1
- import type {NumberAbsolute, BuildTuple, And, Or} from './internal';
1
+ import type {NumberAbsolute, BuildTuple} from './internal';
2
2
  import type {IsEqual} from './is-equal';
3
3
  import type {PositiveInfinity, NegativeInfinity, IsNegative} from './numeric';
4
4
  import type {LessThan} from './less-than';
5
5
  import type {Sum} from './sum';
6
+ import type {And} from './and';
7
+ import type {Or} from './or';
6
8
 
7
9
  /**
8
10
  Returns the difference between two numbers.
package/source/sum.d.ts CHANGED
@@ -1,7 +1,9 @@
1
- import type {NumberAbsolute, BuildTuple, And, Or, ArrayMax, ArrayMin} from './internal';
1
+ import type {NumberAbsolute, BuildTuple, ArrayMax, ArrayMin} from './internal';
2
2
  import type {IsEqual} from './is-equal';
3
3
  import type {PositiveInfinity, NegativeInfinity, IsNegative} from './numeric';
4
4
  import type {Subtract} from './subtract';
5
+ import type {And} from './and';
6
+ import type {Or} from './or';
5
7
 
6
8
  /**
7
9
  Returns the sum of two numbers.
@@ -19,6 +19,7 @@ declare namespace TsConfigJson {
19
19
  | 'ESNext'
20
20
  | 'Node16'
21
21
  | 'NodeNext'
22
+ | 'Preserve'
22
23
  | 'None'
23
24
  // Lowercase alternatives
24
25
  | 'commonjs'
@@ -32,6 +33,7 @@ declare namespace TsConfigJson {
32
33
  | 'esnext'
33
34
  | 'node16'
34
35
  | 'nodenext'
36
+ | 'preserve'
35
37
  | 'none';
36
38
 
37
39
  export type NewLine =
@@ -113,6 +115,14 @@ declare namespace TsConfigJson {
113
115
  | 'ES2021.Promise'
114
116
  | 'ES2021.String'
115
117
  | 'ES2021.WeakRef'
118
+ | 'ES2022'
119
+ | 'ES2022.Array'
120
+ | 'ES2022.Error'
121
+ | 'ES2022.Intl'
122
+ | 'ES2022.Object'
123
+ | 'ES2022.SharedMemory'
124
+ | 'ES2022.String'
125
+ | 'ES2022.RegExp'
116
126
  | 'ESNext'
117
127
  | 'ESNext.Array'
118
128
  | 'ESNext.AsyncIterable'
@@ -172,6 +182,14 @@ declare namespace TsConfigJson {
172
182
  | 'es2021.promise'
173
183
  | 'es2021.string'
174
184
  | 'es2021.weakref'
185
+ | 'es2022'
186
+ | 'es2022.array'
187
+ | 'es2022.error'
188
+ | 'es2022.intl'
189
+ | 'es2022.object'
190
+ | 'es2022.sharedmemory'
191
+ | 'es2022.string'
192
+ | 'es2022.regexp'
175
193
  | 'esnext'
176
194
  | 'esnext.array'
177
195
  | 'esnext.asynciterable'