type-fest 4.14.0 → 4.16.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
@@ -9,6 +9,7 @@ export type {KeysOfUnion} from './source/keys-of-union';
9
9
  export type {DistributedOmit} from './source/distributed-omit';
10
10
  export type {DistributedPick} from './source/distributed-pick';
11
11
  export type {EmptyObject, IsEmptyObject} from './source/empty-object';
12
+ export type {IfEmptyObject} from './source/if-empty-object';
12
13
  export type {NonEmptyObject} from './source/non-empty-object';
13
14
  export type {UnknownRecord} from './source/unknown-record';
14
15
  export type {UnknownArray} from './source/unknown-array';
@@ -23,6 +24,7 @@ export type {RequireAtLeastOne} from './source/require-at-least-one';
23
24
  export type {RequireExactlyOne} from './source/require-exactly-one';
24
25
  export type {RequireAllOrNone} from './source/require-all-or-none';
25
26
  export type {RequireOneOrNone} from './source/require-one-or-none';
27
+ export type {SingleKeyObject} from './source/single-key-object';
26
28
  export type {OmitIndexSignature} from './source/omit-index-signature';
27
29
  export type {PickIndexSignature} from './source/pick-index-signature';
28
30
  export type {PartialDeep, PartialDeepOptions} from './source/partial-deep';
@@ -96,6 +98,8 @@ export type {HasReadonlyKeys} from './source/has-readonly-keys';
96
98
  export type {WritableKeysOf} from './source/writable-keys-of';
97
99
  export type {HasWritableKeys} from './source/has-writable-keys';
98
100
  export type {Spread} from './source/spread';
101
+ export type {IsInteger} from './source/is-integer';
102
+ export type {IsFloat} from './source/is-float';
99
103
  export type {TupleToUnion} from './source/tuple-to-union';
100
104
  export type {IntRange} from './source/int-range';
101
105
  export type {IsEqual} from './source/is-equal';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-fest",
3
- "version": "4.14.0",
3
+ "version": "4.16.0",
4
4
  "description": "A collection of essential TypeScript types",
5
5
  "license": "(MIT OR CC0-1.0)",
6
6
  "repository": "sindresorhus/type-fest",
@@ -16,7 +16,6 @@
16
16
  "node": ">=16"
17
17
  },
18
18
  "scripts": {
19
- "test:set-parameter-type": "tsc --noEmit test-d/set-parameter-type",
20
19
  "test:source-files-extension": "node script/test/source-files-extension.js",
21
20
  "test:tsc": "tsc",
22
21
  "test:tsd": "tsd",
@@ -40,11 +39,10 @@
40
39
  "generics"
41
40
  ],
42
41
  "devDependencies": {
43
- "@sindresorhus/tsconfig": "~0.7.0",
44
42
  "expect-type": "^0.15.0",
45
43
  "npm-run-all2": "^6.1.2",
46
- "tsd": "^0.28.1",
47
- "typescript": "^5.2.2",
44
+ "tsd": "^0.31.0",
45
+ "typescript": "~5.4.3",
48
46
  "xo": "^0.58.0"
49
47
  },
50
48
  "xo": {
package/readme.md CHANGED
@@ -49,6 +49,22 @@
49
49
  </a>
50
50
  <br>
51
51
  <br>
52
+ <br>
53
+ <a href="https://logto.io/?ref=sindre">
54
+ <div>
55
+ <picture>
56
+ <source width="200" media="(prefers-color-scheme: dark)" srcset="https://sindresorhus.com/assets/thanks/logto-logo-dark.svg?x">
57
+ <source width="200" media="(prefers-color-scheme: light)" srcset="https://sindresorhus.com/assets/thanks/logto-logo-light.svg?x">
58
+ <img width="200" src="https://sindresorhus.com/assets/thanks/logto-logo-light.svg?x" alt="Logto logo">
59
+ </picture>
60
+ </div>
61
+ <b>The better identity infrastructure for developers</b>
62
+ <div>
63
+ <sup>Logto is an open-source Auth0 alternative designed for every app.</sup>
64
+ </div>
65
+ </a>
66
+ <br>
67
+ <br>
52
68
  </p>
53
69
  </div>
54
70
  <br>
@@ -110,7 +126,6 @@ Click the type names for complete docs.
110
126
  ### Utilities
111
127
 
112
128
  - [`EmptyObject`](source/empty-object.d.ts) - Represents a strictly empty plain object, the `{}` value.
113
- - [`IsEmptyObject`](source/empty-object.d.ts) - Returns a `boolean` for whether the type is strictly equal to an empty plain object, the `{}` value.
114
129
  - [`NonEmptyObject`](source/non-empty-object.d.ts) - Represents an object with at least 1 non-optional key.
115
130
  - [`UnknownRecord`](source/unknown-record.d.ts) - Represents an object with `unknown` value. You probably want this instead of `{}`.
116
131
  - [`UnknownArray`](source/unknown-array.d.ts) - Represents an array with `unknown` value.
@@ -125,6 +140,7 @@ Click the type names for complete docs.
125
140
  - [`RequireExactlyOne`](source/require-exactly-one.d.ts) - Create a type that requires exactly a single key of the given keys and disallows more.
126
141
  - [`RequireAllOrNone`](source/require-all-or-none.d.ts) - Create a type that requires all of the given keys or none of the given keys.
127
142
  - [`RequireOneOrNone`](source/require-one-or-none.d.ts) - Create a type that requires exactly a single key of the given keys and disallows more, or none of the given keys.
143
+ - [`SingleKeyObject`](source/single-key-object.d.ts) - Create a type that only accepts an object with a single key.
128
144
  - [`RequiredDeep`](source/required-deep.d.ts) - Create a deeply required version of another type. Use [`Required<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#requiredtype) if you only need one level deep.
129
145
  - [`PickDeep`](source/pick-deep.d.ts) - Pick properties from a deeply-nested object. Use [`Pick<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#picktype-keys) if you only need one level deep.
130
146
  - [`OmitDeep`](source/omit-deep.d.ts) - Omit properties from a deeply-nested object. Use [`Omit<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys) if you only need one level deep.
@@ -220,6 +236,7 @@ type ShouldBeNever = IfAny<'not any', 'not never', 'never'>;
220
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).)
221
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).)
222
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
+ - [`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))
223
240
 
224
241
  ### JSON
225
242
 
@@ -268,6 +285,8 @@ type ShouldBeNever = IfAny<'not any', 'not never', 'never'>;
268
285
  - [`NegativeInteger`](source/numeric.d.ts) - A negative (`-∞ < x < 0`) `number` that is an integer.
269
286
  - [`NonNegativeInteger`](source/numeric.d.ts) - A non-negative (`0 <= x < ∞`) `number` that is an integer.
270
287
  - [`IsNegative`](source/numeric.d.ts) - Returns a boolean for whether the given number is a negative number.
288
+ - [`IsFloat`](source/is-float.d.ts) - Returns a boolean for whether the given number is a float, like `1.5` or `-1.5`.
289
+ - [`IsInteger`](source/is-integer.d.ts) - Returns a boolean for whether the given number is a integer, like `-5`, `1.0` or `100`.
271
290
  - [`GreaterThan`](source/greater-than.d.ts) - Returns a boolean for whether a given number is greater than another number.
272
291
  - [`GreaterThanOrEqual`](source/greater-than-or-equal.d.ts) - Returns a boolean for whether a given number is greater than or equal to another number.
273
292
  - [`LessThan`](source/less-than.d.ts) - Returns a boolean for whether a given number is less than another number.
@@ -30,11 +30,10 @@ type StringKeysAndUndefined = ConditionalKeys<Example, string | undefined>;
30
30
 
31
31
  @category Object
32
32
  */
33
- export type ConditionalKeys<Base, Condition> = NonNullable<
34
- // Wrap in `NonNullable` to strip away the `undefined` type from the produced union.
33
+ export type ConditionalKeys<Base, Condition> =
35
34
  {
36
35
  // Map through all the keys of the given base type.
37
- [Key in keyof Base]:
36
+ [Key in keyof Base]-?:
38
37
  // Pick only keys with types extending the given `Condition` type.
39
38
  Base[Key] extends Condition
40
39
  // Retain this key since the condition passes.
@@ -43,5 +42,4 @@ export type ConditionalKeys<Base, Condition> = NonNullable<
43
42
  : never;
44
43
 
45
44
  // Convert the produced object into a union type of the keys which passed the conditional test.
46
- }[keyof Base]
47
- >;
45
+ }[keyof Base];
@@ -0,0 +1,26 @@
1
+ import type {IsEmptyObject} from './empty-object';
2
+
3
+ /**
4
+ An if-else-like type that resolves depending on whether the given type is `{}`.
5
+
6
+ @see {@link IsEmptyObject}
7
+
8
+ @example
9
+ ```
10
+ import type {IfEmptyObject} from 'type-fest';
11
+
12
+ type ShouldBeTrue = IfEmptyObject<{}>;
13
+ //=> true
14
+
15
+ type ShouldBeBar = IfEmptyObject<{key: any}, 'foo', 'bar'>;
16
+ //=> 'bar'
17
+ ```
18
+
19
+ @category Type Guard
20
+ @category Utilities
21
+ */
22
+ export type IfEmptyObject<
23
+ T,
24
+ TypeIfEmptyObject = true,
25
+ TypeIfNotEmptyObject = false,
26
+ > = IsEmptyObject<T> extends true ? TypeIfEmptyObject : TypeIfNotEmptyObject;
@@ -416,9 +416,11 @@ Multiple call signatures cannot currently be supported due to a TypeScript limit
416
416
  @see https://github.com/microsoft/TypeScript/issues/29732
417
417
  */
418
418
  export type HasMultipleCallSignatures<T extends (...arguments_: any[]) => unknown> =
419
- T extends {(...arguments_: infer A): unknown; (...arguments_: any[]): unknown}
420
- ? unknown[] extends A
421
- ? false
419
+ T extends {(...arguments_: infer A): unknown; (...arguments_: infer B): unknown}
420
+ ? B extends A
421
+ ? A extends B
422
+ ? false
423
+ : true
422
424
  : true
423
425
  : false;
424
426
 
@@ -0,0 +1,33 @@
1
+ import type {Zero} from './numeric';
2
+
3
+ /**
4
+ Returns a boolean for whether the given number is a float, like `1.5` or `-1.5`.
5
+
6
+ It returns `false` for `Infinity`.
7
+
8
+ Use-case:
9
+ - If you want to make a conditional branch based on the result of whether a number is a float or not.
10
+
11
+ @example
12
+ ```
13
+ type Float = IsFloat<1.5>;
14
+ //=> true
15
+
16
+ type IntegerWithDecimal = IsInteger<1.0>;
17
+ //=> false
18
+
19
+ type NegativeFloat = IsInteger<-1.5>;
20
+ //=> true
21
+
22
+ type Infinity_ = IsInteger<Infinity>;
23
+ //=> false
24
+ ```
25
+ */
26
+ export type IsFloat<T> =
27
+ T extends number
28
+ ? `${T}` extends `${infer _Sign extends '' | '-'}${number}.${infer Decimal extends number}`
29
+ ? Decimal extends Zero
30
+ ? false
31
+ : true
32
+ : false
33
+ : false;
@@ -0,0 +1,48 @@
1
+ import type {Not} from './internal';
2
+ import type {IsFloat} from './is-float';
3
+ import type {PositiveInfinity, NegativeInfinity} from './numeric';
4
+
5
+ /**
6
+ Returns a boolean for whether the given number is a integer, like `-5`, `1.0` or `100`.
7
+
8
+ Like [`Number#IsInteger()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/IsInteger) but for types.
9
+
10
+ Use-case:
11
+ - If you want to make a conditional branch based on the result of whether a number is a intrger or not.
12
+
13
+ @example
14
+ ```
15
+ type Integer = IsInteger<1>;
16
+ //=> true
17
+
18
+ type IntegerWithDecimal = IsInteger<1.0>;
19
+ //=> true
20
+
21
+ type NegativeInteger = IsInteger<-1>;
22
+ //=> true
23
+
24
+ type Float = IsInteger<1.5>;
25
+ //=> false
26
+
27
+ // Supports non-decimal numbers
28
+
29
+ type OctalInteger: IsInteger<0o10>;
30
+ //=> true
31
+
32
+ type BinaryInteger: IsInteger<0b10>;
33
+ //=> true
34
+
35
+ type HexadecimalInteger: IsInteger<0x10>;
36
+ //=> true
37
+ ```
38
+ */
39
+ export type IsInteger<T> =
40
+ T extends bigint
41
+ ? true
42
+ : T extends number
43
+ ? number extends T
44
+ ? false
45
+ : T extends PositiveInfinity | NegativeInfinity
46
+ ? false
47
+ : Not<IsFloat<T>>
48
+ : false;
@@ -1,3 +1,6 @@
1
+ import type {IsFloat} from './is-float';
2
+ import type {IsInteger} from './is-integer';
3
+
1
4
  export type Numeric = number | bigint;
2
5
 
3
6
  type Zero = 0 | 0n;
@@ -49,10 +52,35 @@ export type Finite<T extends number> = T extends PositiveInfinity | NegativeInfi
49
52
 
50
53
  /**
51
54
  A `number` that is an integer.
52
- You can't pass a `bigint` as they are already guaranteed to be integers.
53
55
 
54
56
  Use-case: Validating and documenting parameters.
55
57
 
58
+ @example
59
+ ```
60
+ type Integer = Integer<1>;
61
+ //=> 1
62
+
63
+ type IntegerWithDecimal = Integer<1.0>;
64
+ //=> 1
65
+
66
+ type NegativeInteger = Integer<-1>;
67
+ //=> -1
68
+
69
+ type Float = Integer<1.5>;
70
+ //=> never
71
+
72
+ // Supports non-decimal numbers
73
+
74
+ type OctalInteger: Integer<0o10>;
75
+ //=> 0o10
76
+
77
+ type BinaryInteger: Integer<0b10>;
78
+ //=> 0b10
79
+
80
+ type HexadecimalInteger: Integer<0x10>;
81
+ //=> 0x10
82
+ ```
83
+
56
84
  @example
57
85
  ```
58
86
  import type {Integer} from 'type-fest';
@@ -67,14 +95,18 @@ declare function setYear<T extends number>(length: Integer<T>): void;
67
95
  */
68
96
  // `${bigint}` is a type that matches a valid bigint literal without the `n` (ex. 1, 0b1, 0o1, 0x1)
69
97
  // Because T is a number and not a string we can effectively use this to filter out any numbers containing decimal points
70
- export type Integer<T extends number> = `${T}` extends `${bigint}` ? T : never;
98
+ export type Integer<T> =
99
+ T extends unknown // To distributive type
100
+ ? IsInteger<T> extends true ? T : never
101
+ : never; // Never happens
71
102
 
72
103
  /**
73
104
  A `number` that is not an integer.
74
- You can't pass a `bigint` as they are already guaranteed to be integers.
75
105
 
76
106
  Use-case: Validating and documenting parameters.
77
107
 
108
+ It does not accept `Infinity`.
109
+
78
110
  @example
79
111
  ```
80
112
  import type {Float} from 'type-fest';
@@ -86,7 +118,10 @@ declare function setPercentage<T extends number>(length: Float<T>): void;
86
118
 
87
119
  @category Numeric
88
120
  */
89
- export type Float<T extends number> = T extends Integer<T> ? never : T;
121
+ export type Float<T> =
122
+ T extends unknown // To distributive type
123
+ ? IsFloat<T> extends true ? T : never
124
+ : never; // Never happens
90
125
 
91
126
  /**
92
127
  A negative (`-∞ < x < 0`) `number` that is not an integer.
@@ -0,0 +1,29 @@
1
+ import type {IfEmptyObject} from './if-empty-object';
2
+ import type {IsUnion} from './internal';
3
+
4
+ /**
5
+ Create a type that only accepts an object with a single key.
6
+
7
+ @example
8
+ ```
9
+ import type {SingleKeyObject} from 'type-fest';
10
+
11
+ const someFunction = <T>(parameter: SingleKeyObject<T>) => {};
12
+
13
+ someFunction({
14
+ value: true
15
+ });
16
+
17
+ someFunction({
18
+ value: true,
19
+ otherKey: true
20
+ });
21
+ // Error: Argument of type '{value: boolean; otherKey: boolean}' is not assignable to parameter of type 'never'.ts(2345)
22
+ ```
23
+
24
+ @category Object
25
+ */
26
+ export type SingleKeyObject<ObjectType> =
27
+ IsUnion<keyof ObjectType> extends true
28
+ ? never
29
+ : IfEmptyObject<ObjectType, never, ObjectType>;