type-fest 4.39.1 → 4.40.1
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 +3 -0
- package/package.json +3 -3
- package/readme.md +3 -10
- package/source/camel-cased-properties-deep.d.ts +5 -6
- package/source/delimiter-cased-properties-deep.d.ts +7 -7
- package/source/is-float.d.ts +24 -14
- package/source/is-integer.d.ts +24 -14
- package/source/non-empty-string.d.ts +28 -0
- package/source/partial-deep.d.ts +29 -23
- package/source/pascal-cased-properties-deep.d.ts +6 -3
- package/source/unknown-map.d.ts +24 -0
- package/source/unknown-set.d.ts +24 -0
package/index.d.ts
CHANGED
|
@@ -11,8 +11,11 @@ export type {DistributedPick} from './source/distributed-pick';
|
|
|
11
11
|
export type {EmptyObject, IsEmptyObject} from './source/empty-object';
|
|
12
12
|
export type {IfEmptyObject} from './source/if-empty-object';
|
|
13
13
|
export type {NonEmptyObject} from './source/non-empty-object';
|
|
14
|
+
export type {NonEmptyString} from './source/non-empty-string';
|
|
14
15
|
export type {UnknownRecord} from './source/unknown-record';
|
|
15
16
|
export type {UnknownArray} from './source/unknown-array';
|
|
17
|
+
export type {UnknownSet} from './source/unknown-set';
|
|
18
|
+
export type {UnknownMap} from './source/unknown-map';
|
|
16
19
|
export type {Except} from './source/except';
|
|
17
20
|
export type {TaggedUnion} from './source/tagged-union';
|
|
18
21
|
export type {Writable} from './source/writable';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "type-fest",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.40.1",
|
|
4
4
|
"description": "A collection of essential TypeScript types",
|
|
5
5
|
"license": "(MIT OR CC0-1.0)",
|
|
6
6
|
"repository": "sindresorhus/type-fest",
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"expect-type": "^1.1.0",
|
|
45
45
|
"npm-run-all2": "^7.0.1",
|
|
46
|
-
"tsd": "^0.
|
|
47
|
-
"typescript": "~5.8.
|
|
46
|
+
"tsd": "^0.32.0",
|
|
47
|
+
"typescript": "~5.8.3",
|
|
48
48
|
"xo": "^0.60.0"
|
|
49
49
|
},
|
|
50
50
|
"xo": {
|
package/readme.md
CHANGED
|
@@ -33,16 +33,6 @@
|
|
|
33
33
|
</a>
|
|
34
34
|
<br>
|
|
35
35
|
<br>
|
|
36
|
-
<a href="https://transloadit.com?utm_source=sindresorhus&utm_medium=referral&utm_campaign=sponsorship&utm_content=type-fest">
|
|
37
|
-
<picture>
|
|
38
|
-
<source width="350" media="(prefers-color-scheme: dark)" srcset="https://sindresorhus.com/assets/thanks/transloadit-logo-dark.svg">
|
|
39
|
-
<source width="350" media="(prefers-color-scheme: light)" srcset="https://sindresorhus.com/assets/thanks/transloadit-logo.svg">
|
|
40
|
-
<img width="350" src="https://sindresorhus.com/assets/thanks/transloadit-logo.svg" alt="Transloadit logo">
|
|
41
|
-
</picture>
|
|
42
|
-
</a>
|
|
43
|
-
<br>
|
|
44
|
-
<br>
|
|
45
|
-
<br>
|
|
46
36
|
<a href="https://logto.io/?ref=sindre">
|
|
47
37
|
<div>
|
|
48
38
|
<picture>
|
|
@@ -129,6 +119,8 @@ Click the type names for complete docs.
|
|
|
129
119
|
- [`NonEmptyObject`](source/non-empty-object.d.ts) - Represents an object with at least 1 non-optional key.
|
|
130
120
|
- [`UnknownRecord`](source/unknown-record.d.ts) - Represents an object with `unknown` value. You probably want this instead of `{}`.
|
|
131
121
|
- [`UnknownArray`](source/unknown-array.d.ts) - Represents an array with `unknown` value.
|
|
122
|
+
- [`UnknownMap`](source/unknown-map.d.ts) - Represents a map with `unknown` key and value.
|
|
123
|
+
- [`UnknownSet`](source/unknown-set.d.ts) - Represents a set with `unknown` value.
|
|
132
124
|
- [`Except`](source/except.d.ts) - Create a type from an object type without certain keys. This is a stricter version of [`Omit`](https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys).
|
|
133
125
|
- [`Writable`](source/writable.d.ts) - Create a type that strips `readonly` from the given type. Inverse of `Readonly<T>`.
|
|
134
126
|
- [`WritableDeep`](source/writable-deep.d.ts) - Create a deeply mutable version of an `object`/`ReadonlyMap`/`ReadonlySet`/`ReadonlyArray` type. The inverse of `ReadonlyDeep<T>`. Use `Writable<T>` if you only need one level deep.
|
|
@@ -207,6 +199,7 @@ Click the type names for complete docs.
|
|
|
207
199
|
- [`And`](source/and.d.ts) - Returns a boolean for whether two given types are both true.
|
|
208
200
|
- [`Or`](source/or.d.ts) - Returns a boolean for whether either of two given types are true.
|
|
209
201
|
- [`NonEmptyTuple`](source/non-empty-tuple.d.ts) - Matches any non-empty tuple.
|
|
202
|
+
- [`NonEmptyString`](source/non-empty-string.d.ts) - Matches any non-empty string.
|
|
210
203
|
- [`FindGlobalType`](source/find-global-type.d.ts) - Tries to find the type of a global with the given name.
|
|
211
204
|
- [`FindGlobalInstanceType`](source/find-global-type.d.ts) - Tries to find one or more types from their globally-defined constructors.
|
|
212
205
|
|
|
@@ -68,12 +68,11 @@ type _CamelCasedPropertiesDeep<
|
|
|
68
68
|
? CamelCasedPropertiesArrayDeep<Value, Options>
|
|
69
69
|
: Value extends Set<infer U>
|
|
70
70
|
? Set<_CamelCasedPropertiesDeep<U, Options>>
|
|
71
|
-
:
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
};
|
|
71
|
+
: Value extends object
|
|
72
|
+
? {
|
|
73
|
+
[K in keyof Value as CamelCase<K, Options>]: _CamelCasedPropertiesDeep<Value[K], Options>;
|
|
74
|
+
}
|
|
75
|
+
: Value;
|
|
77
76
|
|
|
78
77
|
// This is a copy of DelimiterCasedPropertiesArrayDeep (see: delimiter-cased-properties-deep.d.ts).
|
|
79
78
|
// These types should be kept in sync.
|
|
@@ -72,13 +72,13 @@ type _DelimiterCasedPropertiesDeep<
|
|
|
72
72
|
: Value extends UnknownArray
|
|
73
73
|
? DelimiterCasedPropertiesArrayDeep<Value, Delimiter, Options>
|
|
74
74
|
: Value extends Set<infer U>
|
|
75
|
-
? Set<_DelimiterCasedPropertiesDeep<U, Delimiter, Options>>
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
75
|
+
? Set<_DelimiterCasedPropertiesDeep<U, Delimiter, Options>>
|
|
76
|
+
: Value extends object
|
|
77
|
+
? {
|
|
78
|
+
[K in keyof Value as DelimiterCase<K, Delimiter, Options>]:
|
|
79
|
+
_DelimiterCasedPropertiesDeep<Value[K], Delimiter, Options>
|
|
80
|
+
}
|
|
81
|
+
: Value;
|
|
82
82
|
|
|
83
83
|
// This is a copy of CamelCasedPropertiesArrayDeep (see: camel-cased-properties-deep.d.ts).
|
|
84
84
|
// These types should be kept in sync.
|
package/source/is-float.d.ts
CHANGED
|
@@ -3,31 +3,41 @@ import type {Zero} from './numeric';
|
|
|
3
3
|
/**
|
|
4
4
|
Returns a boolean for whether the given number is a float, like `1.5` or `-1.5`.
|
|
5
5
|
|
|
6
|
-
It returns `false` for `Infinity`.
|
|
7
|
-
|
|
8
6
|
Use-case:
|
|
9
7
|
- If you want to make a conditional branch based on the result of whether a number is a float or not.
|
|
10
8
|
|
|
11
9
|
@example
|
|
12
10
|
```
|
|
13
|
-
type
|
|
11
|
+
import type {IsFloat, PositiveInfinity} from "type-fest";
|
|
12
|
+
|
|
13
|
+
type A = IsFloat<1.5>;
|
|
14
14
|
//=> true
|
|
15
15
|
|
|
16
|
-
type
|
|
17
|
-
//=>
|
|
16
|
+
type B = IsFloat<-1.5>;
|
|
17
|
+
//=> true
|
|
18
18
|
|
|
19
|
-
type
|
|
19
|
+
type C = IsFloat<1e-7>;
|
|
20
20
|
//=> true
|
|
21
21
|
|
|
22
|
-
type
|
|
22
|
+
type D = IsFloat<1.0>;
|
|
23
|
+
//=> false
|
|
24
|
+
|
|
25
|
+
type E = IsFloat<PositiveInfinity>;
|
|
26
|
+
//=> false
|
|
27
|
+
|
|
28
|
+
type F = IsFloat<1.23e+21>;
|
|
23
29
|
//=> false
|
|
24
30
|
```
|
|
31
|
+
|
|
32
|
+
@category Type Guard
|
|
33
|
+
@category Numeric
|
|
25
34
|
*/
|
|
26
|
-
export type IsFloat<T> =
|
|
27
|
-
T extends number
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
35
|
+
export type IsFloat<T> = T extends number
|
|
36
|
+
? `${T}` extends `${number}e${infer E extends '-' | '+'}${number}`
|
|
37
|
+
? E extends '-'
|
|
38
|
+
? true
|
|
39
|
+
: false
|
|
40
|
+
: `${T}` extends `${number}.${number}`
|
|
41
|
+
? true
|
|
42
|
+
: false
|
|
33
43
|
: false;
|
package/source/is-integer.d.ts
CHANGED
|
@@ -3,38 +3,48 @@ import type {IsFloat} from './is-float';
|
|
|
3
3
|
import type {PositiveInfinity, NegativeInfinity} from './numeric';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
Returns a boolean for whether the given number is
|
|
7
|
-
|
|
8
|
-
Like [`Number#IsInteger()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/IsInteger) but for types.
|
|
6
|
+
Returns a boolean for whether the given number is an integer, like `-5`, `1.0`, or `100`.
|
|
9
7
|
|
|
10
8
|
Use-case:
|
|
11
|
-
- If you want to make a conditional branch based on the result of whether a number is
|
|
9
|
+
- If you want to make a conditional branch based on the result of whether a number is an integer or not.
|
|
12
10
|
|
|
13
11
|
@example
|
|
14
12
|
```
|
|
15
|
-
type
|
|
16
|
-
//=> true
|
|
13
|
+
import type {IsInteger, PositiveInfinity} from "type-fest";
|
|
17
14
|
|
|
18
|
-
type
|
|
15
|
+
type A = IsInteger<1>;
|
|
19
16
|
//=> true
|
|
20
17
|
|
|
21
|
-
type
|
|
18
|
+
type B = IsInteger<1.0>;
|
|
22
19
|
//=> true
|
|
23
20
|
|
|
24
|
-
type
|
|
25
|
-
//=>
|
|
21
|
+
type C = IsInteger<-1>;
|
|
22
|
+
//=> true
|
|
26
23
|
|
|
27
|
-
|
|
24
|
+
type D = IsInteger<0b10>;
|
|
25
|
+
//=> true
|
|
28
26
|
|
|
29
|
-
type
|
|
27
|
+
type E = IsInteger<0o10>;
|
|
30
28
|
//=> true
|
|
31
29
|
|
|
32
|
-
type
|
|
30
|
+
type F = IsInteger<0x10>;
|
|
33
31
|
//=> true
|
|
34
32
|
|
|
35
|
-
type
|
|
33
|
+
type G = IsInteger<1.23+21>;
|
|
36
34
|
//=> true
|
|
35
|
+
|
|
36
|
+
type H = IsInteger<1.5>;
|
|
37
|
+
//=> false
|
|
38
|
+
|
|
39
|
+
type I = IsInteger<PositiveInfinity>;
|
|
40
|
+
//=> false
|
|
41
|
+
|
|
42
|
+
type J = IsInteger<1e-7>;
|
|
43
|
+
//=> false
|
|
37
44
|
```
|
|
45
|
+
|
|
46
|
+
@category Type Guard
|
|
47
|
+
@category Numeric
|
|
38
48
|
*/
|
|
39
49
|
export type IsInteger<T> =
|
|
40
50
|
T extends bigint
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Matches any non-empty string.
|
|
3
|
+
|
|
4
|
+
This is useful when you need a string that is not empty, for example, as a function parameter.
|
|
5
|
+
|
|
6
|
+
NOTE:
|
|
7
|
+
- This returns `never` not just when instantiated with an empty string, but also when an empty string is a subtype of the instantiated type, like `string` or `Uppercase<string>`.
|
|
8
|
+
|
|
9
|
+
@example
|
|
10
|
+
```
|
|
11
|
+
import type {NonEmptyString} from 'type-fest';
|
|
12
|
+
|
|
13
|
+
declare function foo<T extends string>(string: NonEmptyString<T>): void;
|
|
14
|
+
|
|
15
|
+
foo('a');
|
|
16
|
+
//=> OK
|
|
17
|
+
|
|
18
|
+
foo('');
|
|
19
|
+
//=> Error: Argument of type '""' is not assignable to parameter of type 'never'.
|
|
20
|
+
|
|
21
|
+
declare const someString: string
|
|
22
|
+
foo(someString);
|
|
23
|
+
//=> Error: Argument of type 'string' is not assignable to parameter of type 'never'.
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
@category String
|
|
27
|
+
*/
|
|
28
|
+
export type NonEmptyString<T extends string> = '' extends T ? never : T;
|
package/source/partial-deep.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type {ApplyDefaultOptions, BuiltIns} from './internal';
|
|
2
|
+
import type {IsNever} from './is-never';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
@see {@link PartialDeep}
|
|
@@ -95,27 +96,29 @@ const partialSettings: PartialDeep<Settings, {recurseIntoArrays: true}> = {
|
|
|
95
96
|
export type PartialDeep<T, Options extends PartialDeepOptions = {}> =
|
|
96
97
|
_PartialDeep<T, ApplyDefaultOptions<PartialDeepOptions, DefaultPartialDeepOptions, Options>>;
|
|
97
98
|
|
|
98
|
-
type _PartialDeep<T, Options extends Required<PartialDeepOptions>> = T extends BuiltIns | ((
|
|
99
|
+
type _PartialDeep<T, Options extends Required<PartialDeepOptions>> = T extends BuiltIns | ((new (...arguments_: any[]) => unknown))
|
|
99
100
|
? T
|
|
100
|
-
: T extends
|
|
101
|
-
?
|
|
102
|
-
: T extends
|
|
103
|
-
?
|
|
104
|
-
: T extends
|
|
105
|
-
?
|
|
106
|
-
: T extends
|
|
107
|
-
?
|
|
108
|
-
: T extends
|
|
109
|
-
?
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
101
|
+
: IsNever<keyof T> extends true // For functions with no properties
|
|
102
|
+
? T
|
|
103
|
+
: T extends Map<infer KeyType, infer ValueType>
|
|
104
|
+
? PartialMapDeep<KeyType, ValueType, Options>
|
|
105
|
+
: T extends Set<infer ItemType>
|
|
106
|
+
? PartialSetDeep<ItemType, Options>
|
|
107
|
+
: T extends ReadonlyMap<infer KeyType, infer ValueType>
|
|
108
|
+
? PartialReadonlyMapDeep<KeyType, ValueType, Options>
|
|
109
|
+
: T extends ReadonlySet<infer ItemType>
|
|
110
|
+
? PartialReadonlySetDeep<ItemType, Options>
|
|
111
|
+
: T extends object
|
|
112
|
+
? T extends ReadonlyArray<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156
|
|
113
|
+
? Options['recurseIntoArrays'] extends true
|
|
114
|
+
? ItemType[] extends T // Test for arrays (non-tuples) specifically
|
|
115
|
+
? readonly ItemType[] extends T // Differentiate readonly and mutable arrays
|
|
116
|
+
? ReadonlyArray<_PartialDeep<Options['allowUndefinedInNonTupleArrays'] extends false ? ItemType : ItemType | undefined, Options>>
|
|
117
|
+
: Array<_PartialDeep<Options['allowUndefinedInNonTupleArrays'] extends false ? ItemType : ItemType | undefined, Options>>
|
|
118
|
+
: PartialObjectDeep<T, Options> // Tuples behave properly
|
|
119
|
+
: T // If they don't opt into array testing, just use the original type
|
|
120
|
+
: PartialObjectDeep<T, Options>
|
|
121
|
+
: unknown;
|
|
119
122
|
|
|
120
123
|
/**
|
|
121
124
|
Same as `PartialDeep`, but accepts only `Map`s and as inputs. Internal helper for `PartialDeep`.
|
|
@@ -140,6 +143,9 @@ type PartialReadonlySetDeep<T, Options extends Required<PartialDeepOptions>> = {
|
|
|
140
143
|
/**
|
|
141
144
|
Same as `PartialDeep`, but accepts only `object`s as inputs. Internal helper for `PartialDeep`.
|
|
142
145
|
*/
|
|
143
|
-
type PartialObjectDeep<ObjectType extends object, Options extends Required<PartialDeepOptions>> =
|
|
144
|
-
|
|
145
|
-
|
|
146
|
+
type PartialObjectDeep<ObjectType extends object, Options extends Required<PartialDeepOptions>> =
|
|
147
|
+
(ObjectType extends (...arguments_: any) => unknown
|
|
148
|
+
? (...arguments_: Parameters<ObjectType>) => ReturnType<ObjectType>
|
|
149
|
+
: {}) & ({
|
|
150
|
+
[KeyType in keyof ObjectType]?: _PartialDeep<ObjectType[KeyType], Options>
|
|
151
|
+
});
|
|
@@ -54,6 +54,9 @@ type _PascalCasedPropertiesDeep<Value, Options extends Required<CamelCaseOptions
|
|
|
54
54
|
: Value extends Array<infer U>
|
|
55
55
|
? Array<_PascalCasedPropertiesDeep<U, Options>>
|
|
56
56
|
: Value extends Set<infer U>
|
|
57
|
-
? Set<_PascalCasedPropertiesDeep<U, Options>>
|
|
58
|
-
|
|
59
|
-
|
|
57
|
+
? Set<_PascalCasedPropertiesDeep<U, Options>>
|
|
58
|
+
: Value extends object
|
|
59
|
+
? {
|
|
60
|
+
[K in keyof Value as PascalCase<K, Options>]: _PascalCasedPropertiesDeep<Value[K], Options>;
|
|
61
|
+
}
|
|
62
|
+
: Value;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Represents a map with `unknown` key and value.
|
|
3
|
+
|
|
4
|
+
Use case: You want a type that all maps can be assigned to, but you don't care about the value.
|
|
5
|
+
|
|
6
|
+
@example
|
|
7
|
+
```
|
|
8
|
+
import type {UnknownMap} from 'type-fest';
|
|
9
|
+
|
|
10
|
+
type IsMap<T> = T extends UnknownMap ? true : false;
|
|
11
|
+
|
|
12
|
+
type A = IsMap<Map<string, number>>;
|
|
13
|
+
//=> true
|
|
14
|
+
|
|
15
|
+
type B = IsMap<ReadonlyMap<number, string>>;
|
|
16
|
+
//=> true
|
|
17
|
+
|
|
18
|
+
type C = IsMap<string>;
|
|
19
|
+
//=> false
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
@category Type
|
|
23
|
+
*/
|
|
24
|
+
export type UnknownMap = ReadonlyMap<unknown, unknown>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Represents a set with `unknown` value.
|
|
3
|
+
|
|
4
|
+
Use case: You want a type that all sets can be assigned to, but you don't care about the value.
|
|
5
|
+
|
|
6
|
+
@example
|
|
7
|
+
```
|
|
8
|
+
import type {UnknownSet} from 'type-fest';
|
|
9
|
+
|
|
10
|
+
type IsSet<T> = T extends UnknownSet ? true : false;
|
|
11
|
+
|
|
12
|
+
type A = IsSet<Set<string>>;
|
|
13
|
+
//=> true
|
|
14
|
+
|
|
15
|
+
type B = IsSet<ReadonlySet<number>>;
|
|
16
|
+
//=> true
|
|
17
|
+
|
|
18
|
+
type C = IsSet<string>;
|
|
19
|
+
//=> false
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
@category Type
|
|
23
|
+
*/
|
|
24
|
+
export type UnknownSet = ReadonlySet<unknown>;
|