@depup/type-fest 5.4.4-depup.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/README.md +25 -0
- package/index.d.ts +211 -0
- package/license-cc0 +121 -0
- package/license-mit +9 -0
- package/package.json +85 -0
- package/readme.md +1052 -0
- package/source/all-extend.d.ts +120 -0
- package/source/all-union-fields.d.ts +91 -0
- package/source/and.d.ts +81 -0
- package/source/array-element.d.ts +46 -0
- package/source/array-indices.d.ts +25 -0
- package/source/array-reverse.d.ts +84 -0
- package/source/array-slice.d.ts +132 -0
- package/source/array-splice.d.ts +104 -0
- package/source/array-tail.d.ts +70 -0
- package/source/array-values.d.ts +24 -0
- package/source/arrayable.d.ts +31 -0
- package/source/async-return-type.d.ts +28 -0
- package/source/asyncify.d.ts +25 -0
- package/source/basic.d.ts +38 -0
- package/source/camel-case.d.ts +92 -0
- package/source/camel-cased-properties-deep.d.ts +99 -0
- package/source/camel-cased-properties.d.ts +45 -0
- package/source/characters.d.ts +67 -0
- package/source/conditional-except.d.ts +47 -0
- package/source/conditional-keys.d.ts +63 -0
- package/source/conditional-pick-deep.d.ts +120 -0
- package/source/conditional-pick.d.ts +46 -0
- package/source/conditional-simplify-deep.d.ts +73 -0
- package/source/conditional-simplify.d.ts +50 -0
- package/source/delimiter-case.d.ts +77 -0
- package/source/delimiter-cased-properties-deep.d.ts +108 -0
- package/source/delimiter-cased-properties.d.ts +48 -0
- package/source/distributed-omit.d.ts +96 -0
- package/source/distributed-pick.d.ts +92 -0
- package/source/empty-object.d.ts +51 -0
- package/source/entries.d.ts +64 -0
- package/source/entry.d.ts +67 -0
- package/source/exact.d.ts +73 -0
- package/source/except.d.ts +112 -0
- package/source/exclude-rest-element.d.ts +40 -0
- package/source/exclude-strict.d.ts +51 -0
- package/source/exclusify-union.d.ts +147 -0
- package/source/extends-strict.d.ts +44 -0
- package/source/extract-rest-element.d.ts +30 -0
- package/source/extract-strict.d.ts +51 -0
- package/source/find-global-type.d.ts +68 -0
- package/source/fixed-length-array.d.ts +97 -0
- package/source/get.d.ts +227 -0
- package/source/global-this.d.ts +24 -0
- package/source/globals/index.d.ts +3 -0
- package/source/globals/observable-like.d.ts +78 -0
- package/source/greater-than-or-equal.d.ts +30 -0
- package/source/greater-than.d.ts +58 -0
- package/source/has-optional-keys.d.ts +23 -0
- package/source/has-readonly-keys.d.ts +23 -0
- package/source/has-required-keys.d.ts +61 -0
- package/source/has-writable-keys.d.ts +23 -0
- package/source/if-any.d.ts +28 -0
- package/source/if-empty-object.d.ts +30 -0
- package/source/if-never.d.ts +28 -0
- package/source/if-null.d.ts +28 -0
- package/source/if-unknown.d.ts +28 -0
- package/source/if.d.ts +102 -0
- package/source/includes.d.ts +24 -0
- package/source/int-closed-range.d.ts +47 -0
- package/source/int-range.d.ts +69 -0
- package/source/internal/array.d.ts +151 -0
- package/source/internal/characters.d.ts +65 -0
- package/source/internal/enforce-optional.d.ts +49 -0
- package/source/internal/index.d.ts +11 -0
- package/source/internal/keys.d.ts +100 -0
- package/source/internal/numeric.d.ts +151 -0
- package/source/internal/object.d.ts +292 -0
- package/source/internal/string.d.ts +202 -0
- package/source/internal/tuple.d.ts +79 -0
- package/source/internal/type.d.ts +164 -0
- package/source/invariant-of.d.ts +85 -0
- package/source/is-any.d.ts +31 -0
- package/source/is-equal.d.ts +42 -0
- package/source/is-float.d.ts +43 -0
- package/source/is-integer.d.ts +60 -0
- package/source/is-literal.d.ts +315 -0
- package/source/is-lowercase.d.ts +38 -0
- package/source/is-never.d.ts +56 -0
- package/source/is-null.d.ts +22 -0
- package/source/is-nullable.d.ts +30 -0
- package/source/is-optional-key-of.d.ts +51 -0
- package/source/is-optional.d.ts +28 -0
- package/source/is-readonly-key-of.d.ts +55 -0
- package/source/is-required-key-of.d.ts +51 -0
- package/source/is-tuple.d.ts +92 -0
- package/source/is-undefined.d.ts +22 -0
- package/source/is-union.d.ts +40 -0
- package/source/is-unknown.d.ts +43 -0
- package/source/is-uppercase.d.ts +38 -0
- package/source/is-writable-key-of.d.ts +51 -0
- package/source/iterable-element.d.ts +66 -0
- package/source/join.d.ts +79 -0
- package/source/json-value.d.ts +33 -0
- package/source/jsonifiable.d.ts +37 -0
- package/source/jsonify.d.ts +127 -0
- package/source/kebab-case.d.ts +46 -0
- package/source/kebab-cased-properties-deep.d.ts +65 -0
- package/source/kebab-cased-properties.d.ts +42 -0
- package/source/key-as-string.d.ts +27 -0
- package/source/keys-of-union.d.ts +44 -0
- package/source/last-array-element.d.ts +36 -0
- package/source/less-than-or-equal.d.ts +24 -0
- package/source/less-than.d.ts +28 -0
- package/source/literal-to-primitive-deep.d.ts +71 -0
- package/source/literal-to-primitive.d.ts +38 -0
- package/source/literal-union.d.ts +39 -0
- package/source/merge-deep.d.ts +495 -0
- package/source/merge-exclusive.d.ts +45 -0
- package/source/merge.d.ts +62 -0
- package/source/multidimensional-array.d.ts +38 -0
- package/source/multidimensional-readonly-array.d.ts +38 -0
- package/source/non-empty-object.d.ts +38 -0
- package/source/non-empty-string.d.ts +32 -0
- package/source/non-empty-tuple.d.ts +24 -0
- package/source/numeric.d.ts +226 -0
- package/source/object-merge.d.ts +194 -0
- package/source/omit-deep.d.ts +154 -0
- package/source/omit-index-signature.d.ts +98 -0
- package/source/opaque.d.ts +3 -0
- package/source/optional-keys-of.d.ts +46 -0
- package/source/or.d.ts +89 -0
- package/source/override-properties.d.ts +43 -0
- package/source/package-json.d.ts +710 -0
- package/source/partial-deep.d.ts +155 -0
- package/source/partial-on-undefined-deep.d.ts +81 -0
- package/source/pascal-case.d.ts +51 -0
- package/source/pascal-cased-properties-deep.d.ts +72 -0
- package/source/pascal-cased-properties.d.ts +42 -0
- package/source/paths.d.ts +241 -0
- package/source/pick-deep.d.ts +152 -0
- package/source/pick-index-signature.d.ts +52 -0
- package/source/primitive.d.ts +15 -0
- package/source/promisable.d.ts +27 -0
- package/source/readonly-deep.d.ts +116 -0
- package/source/readonly-keys-of.d.ts +38 -0
- package/source/readonly-tuple.d.ts +34 -0
- package/source/remove-prefix.d.ts +132 -0
- package/source/replace.d.ts +87 -0
- package/source/require-all-or-none.d.ts +54 -0
- package/source/require-at-least-one.d.ts +50 -0
- package/source/require-exactly-one.d.ts +48 -0
- package/source/require-one-or-none.d.ts +49 -0
- package/source/required-deep.d.ts +76 -0
- package/source/required-keys-of.d.ts +38 -0
- package/source/schema.d.ts +119 -0
- package/source/screaming-snake-case.d.ts +30 -0
- package/source/set-field-type.d.ts +67 -0
- package/source/set-non-nullable-deep.d.ts +85 -0
- package/source/set-non-nullable.d.ts +41 -0
- package/source/set-optional.d.ts +46 -0
- package/source/set-parameter-type.d.ts +125 -0
- package/source/set-readonly.d.ts +44 -0
- package/source/set-required-deep.d.ts +64 -0
- package/source/set-required.d.ts +79 -0
- package/source/set-return-type.d.ts +31 -0
- package/source/shared-union-fields-deep.d.ts +180 -0
- package/source/shared-union-fields.d.ts +79 -0
- package/source/simplify-deep.d.ts +117 -0
- package/source/simplify.d.ts +61 -0
- package/source/single-key-object.d.ts +28 -0
- package/source/snake-case.d.ts +47 -0
- package/source/snake-cased-properties-deep.d.ts +65 -0
- package/source/snake-cased-properties.d.ts +42 -0
- package/source/split-on-rest-element.d.ts +106 -0
- package/source/split.d.ts +90 -0
- package/source/spread.d.ts +82 -0
- package/source/string-repeat.d.ts +49 -0
- package/source/string-slice.d.ts +39 -0
- package/source/stringified.d.ts +25 -0
- package/source/structured-cloneable.d.ts +89 -0
- package/source/subtract.d.ts +86 -0
- package/source/sum.d.ts +81 -0
- package/source/tagged-union.d.ts +53 -0
- package/source/tagged.d.ts +263 -0
- package/source/trim.d.ts +29 -0
- package/source/tsconfig-json.d.ts +1324 -0
- package/source/tuple-of.d.ts +80 -0
- package/source/tuple-to-object.d.ts +47 -0
- package/source/tuple-to-union.d.ts +54 -0
- package/source/typed-array.d.ts +19 -0
- package/source/undefined-on-partial-deep.d.ts +83 -0
- package/source/union-to-intersection.d.ts +35 -0
- package/source/union-to-tuple.d.ts +58 -0
- package/source/unknown-array.d.ts +27 -0
- package/source/unknown-map.d.ts +26 -0
- package/source/unknown-record.d.ts +33 -0
- package/source/unknown-set.d.ts +26 -0
- package/source/unwrap-partial.d.ts +33 -0
- package/source/value-of.d.ts +24 -0
- package/source/words.d.ts +122 -0
- package/source/writable-deep.d.ts +84 -0
- package/source/writable-keys-of.d.ts +34 -0
- package/source/writable.d.ts +72 -0
- package/source/xor.d.ts +83 -0
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import type {Primitive} from './primitive.d.ts';
|
|
2
|
+
import type {_Numeric} from './numeric.d.ts';
|
|
3
|
+
import type {CollapseLiterals, IfNotAnyOrNever, IsNotFalse, IsPrimitive} from './internal/index.d.ts';
|
|
4
|
+
import type {IsNever} from './is-never.d.ts';
|
|
5
|
+
import type {TagContainer, UnwrapTagged} from './tagged.d.ts';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
Returns a boolean for whether the given type `T` is the specified `LiteralType`.
|
|
9
|
+
|
|
10
|
+
@link https://stackoverflow.com/a/52806744/10292952
|
|
11
|
+
|
|
12
|
+
@example
|
|
13
|
+
```
|
|
14
|
+
type A = LiteralCheck<1, number>;
|
|
15
|
+
//=> true
|
|
16
|
+
|
|
17
|
+
type B = LiteralCheck<number, number>;
|
|
18
|
+
//=> false
|
|
19
|
+
|
|
20
|
+
type C = LiteralCheck<1, string>;
|
|
21
|
+
//=> false
|
|
22
|
+
```
|
|
23
|
+
*/
|
|
24
|
+
type LiteralCheck<T, LiteralType extends Primitive> = (
|
|
25
|
+
IsNever<T> extends false // Must be wider than `never`
|
|
26
|
+
? [T] extends [LiteralType & infer U] // Remove any branding
|
|
27
|
+
? [U] extends [LiteralType] // Must be narrower than `LiteralType`
|
|
28
|
+
? [LiteralType] extends [U] // Cannot be wider than `LiteralType`
|
|
29
|
+
? false
|
|
30
|
+
: true
|
|
31
|
+
: false
|
|
32
|
+
: false
|
|
33
|
+
: false
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
Returns a boolean for whether the given type `T` is one of the specified literal types in `LiteralUnionType`.
|
|
38
|
+
|
|
39
|
+
@example
|
|
40
|
+
```
|
|
41
|
+
type A = LiteralChecks<1, Numeric>;
|
|
42
|
+
//=> true
|
|
43
|
+
|
|
44
|
+
type B = LiteralChecks<1n, Numeric>;
|
|
45
|
+
//=> true
|
|
46
|
+
|
|
47
|
+
type C = LiteralChecks<bigint, Numeric>;
|
|
48
|
+
//=> false
|
|
49
|
+
```
|
|
50
|
+
*/
|
|
51
|
+
type LiteralChecks<T, LiteralUnionType> = (
|
|
52
|
+
// Conditional type to force union distribution.
|
|
53
|
+
// If `T` is none of the literal types in the union `LiteralUnionType`, then `LiteralCheck<T, LiteralType>` will evaluate to `false` for the whole union.
|
|
54
|
+
// If `T` is one of the literal types in the union, it will evaluate to `boolean` (i.e. `true | false`)
|
|
55
|
+
IsNotFalse<LiteralUnionType extends Primitive
|
|
56
|
+
? LiteralCheck<T, LiteralUnionType>
|
|
57
|
+
: never
|
|
58
|
+
>
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
Returns a boolean for whether the given type is a `string` [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types).
|
|
63
|
+
|
|
64
|
+
Useful for:
|
|
65
|
+
- providing strongly-typed string manipulation functions
|
|
66
|
+
- constraining strings to be a string literal
|
|
67
|
+
- type utilities, such as when constructing parsers and ASTs
|
|
68
|
+
|
|
69
|
+
The implementation of this type is inspired by the trick mentioned in this [StackOverflow answer](https://stackoverflow.com/a/68261113/420747).
|
|
70
|
+
|
|
71
|
+
@example
|
|
72
|
+
```
|
|
73
|
+
import type {IsStringLiteral} from 'type-fest';
|
|
74
|
+
|
|
75
|
+
type CapitalizedString<T extends string> = IsStringLiteral<T> extends true ? Capitalize<T> : string;
|
|
76
|
+
|
|
77
|
+
// https://github.com/yankeeinlondon/native-dash/blob/master/src/capitalize.ts
|
|
78
|
+
function capitalize<T extends Readonly<string>>(input: T): CapitalizedString<T> {
|
|
79
|
+
return (input.slice(0, 1).toUpperCase() + input.slice(1)) as CapitalizedString<T>;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const output = capitalize('hello, world!');
|
|
83
|
+
//=> 'Hello, world!'
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
@example
|
|
87
|
+
```
|
|
88
|
+
// String types with infinite set of possible values return `false`.
|
|
89
|
+
|
|
90
|
+
import type {IsStringLiteral} from 'type-fest';
|
|
91
|
+
|
|
92
|
+
type AllUppercaseStrings = IsStringLiteral<Uppercase<string>>;
|
|
93
|
+
//=> false
|
|
94
|
+
|
|
95
|
+
type StringsStartingWithOn = IsStringLiteral<`on${string}`>;
|
|
96
|
+
//=> false
|
|
97
|
+
|
|
98
|
+
// This behaviour is particularly useful in string manipulation utilities, as infinite string types often require separate handling.
|
|
99
|
+
|
|
100
|
+
type Length<S extends string, Counter extends never[] = []> =
|
|
101
|
+
IsStringLiteral<S> extends false
|
|
102
|
+
? number // return `number` for infinite string types
|
|
103
|
+
: S extends `${string}${infer Tail}`
|
|
104
|
+
? Length<Tail, [...Counter, never]>
|
|
105
|
+
: Counter['length'];
|
|
106
|
+
|
|
107
|
+
type L1 = Length<Lowercase<string>>;
|
|
108
|
+
//=> number
|
|
109
|
+
|
|
110
|
+
type L2 = Length<`${number}`>;
|
|
111
|
+
//=> number
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
@category Type Guard
|
|
115
|
+
@category Utilities
|
|
116
|
+
*/
|
|
117
|
+
export type IsStringLiteral<S> = IfNotAnyOrNever<S,
|
|
118
|
+
_IsStringLiteral<CollapseLiterals<S extends TagContainer<any> ? UnwrapTagged<S> : S>>,
|
|
119
|
+
false, false>;
|
|
120
|
+
|
|
121
|
+
export type _IsStringLiteral<S> =
|
|
122
|
+
// If `T` is an infinite string type (e.g., `on${string}`), `Record<T, never>` produces an index signature,
|
|
123
|
+
// and since `{}` extends index signatures, the result becomes `false`.
|
|
124
|
+
S extends string
|
|
125
|
+
? {} extends Record<S, never>
|
|
126
|
+
? false
|
|
127
|
+
: true
|
|
128
|
+
: false;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
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).
|
|
132
|
+
|
|
133
|
+
Useful for:
|
|
134
|
+
- providing strongly-typed functions when given literal arguments
|
|
135
|
+
- type utilities, such as when constructing parsers and ASTs
|
|
136
|
+
|
|
137
|
+
@example
|
|
138
|
+
```
|
|
139
|
+
import type {IsNumericLiteral, IsStringLiteral} from 'type-fest';
|
|
140
|
+
|
|
141
|
+
// https://github.com/inocan-group/inferred-types/blob/master/modules/types/src/boolean-logic/operators/EndsWith.ts
|
|
142
|
+
type EndsWith<TValue, TEndsWith extends string> =
|
|
143
|
+
TValue extends string
|
|
144
|
+
? IsStringLiteral<TEndsWith> extends true
|
|
145
|
+
? IsStringLiteral<TValue> extends true
|
|
146
|
+
? TValue extends `${string}${TEndsWith}`
|
|
147
|
+
? true
|
|
148
|
+
: false
|
|
149
|
+
: boolean
|
|
150
|
+
: boolean
|
|
151
|
+
: TValue extends number
|
|
152
|
+
? IsNumericLiteral<TValue> extends true
|
|
153
|
+
? EndsWith<`${TValue}`, TEndsWith>
|
|
154
|
+
: false
|
|
155
|
+
: false;
|
|
156
|
+
|
|
157
|
+
function endsWith<Input extends string | number, End extends string>(input: Input, end: End) {
|
|
158
|
+
return `${input}`.endsWith(end) as EndsWith<Input, End>;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
endsWith('abc', 'c');
|
|
162
|
+
//=> true
|
|
163
|
+
|
|
164
|
+
endsWith(123_456, '456');
|
|
165
|
+
//=> true
|
|
166
|
+
|
|
167
|
+
const end = '123' as string;
|
|
168
|
+
|
|
169
|
+
endsWith('abc123', end);
|
|
170
|
+
//=> boolean
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
@category Type Guard
|
|
174
|
+
@category Utilities
|
|
175
|
+
*/
|
|
176
|
+
export type IsNumericLiteral<T> = LiteralChecks<T, _Numeric>;
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
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).
|
|
180
|
+
|
|
181
|
+
Useful for:
|
|
182
|
+
- providing strongly-typed functions when given literal arguments
|
|
183
|
+
- type utilities, such as when constructing parsers and ASTs
|
|
184
|
+
|
|
185
|
+
@example
|
|
186
|
+
```
|
|
187
|
+
import type {IsBooleanLiteral} from 'type-fest';
|
|
188
|
+
|
|
189
|
+
const id = 123;
|
|
190
|
+
|
|
191
|
+
type GetId<AsString extends boolean> =
|
|
192
|
+
IsBooleanLiteral<AsString> extends true
|
|
193
|
+
? AsString extends true
|
|
194
|
+
? `${typeof id}`
|
|
195
|
+
: typeof id
|
|
196
|
+
: number | string;
|
|
197
|
+
|
|
198
|
+
function getId<AsString extends boolean = false>(options?: {asString: AsString}) {
|
|
199
|
+
return (options?.asString ? `${id}` : id) as GetId<AsString>;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const numberId = getId();
|
|
203
|
+
//=> 123
|
|
204
|
+
|
|
205
|
+
const stringId = getId({asString: true});
|
|
206
|
+
//=> '123'
|
|
207
|
+
|
|
208
|
+
declare const runtimeBoolean: boolean;
|
|
209
|
+
const eitherId = getId({asString: runtimeBoolean});
|
|
210
|
+
//=> string | number
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
@category Type Guard
|
|
214
|
+
@category Utilities
|
|
215
|
+
*/
|
|
216
|
+
export type IsBooleanLiteral<T> = LiteralCheck<T, boolean>;
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
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).
|
|
220
|
+
|
|
221
|
+
Useful for:
|
|
222
|
+
- providing strongly-typed functions when given literal arguments
|
|
223
|
+
- type utilities, such as when constructing parsers and ASTs
|
|
224
|
+
|
|
225
|
+
@example
|
|
226
|
+
```
|
|
227
|
+
import type {IsSymbolLiteral} from 'type-fest';
|
|
228
|
+
|
|
229
|
+
type Get<Object_ extends Record<symbol, number>, Key extends keyof Object_> =
|
|
230
|
+
IsSymbolLiteral<Key> extends true
|
|
231
|
+
? Object_[Key]
|
|
232
|
+
: number;
|
|
233
|
+
|
|
234
|
+
function get<Object_ extends Record<symbol, number>, Key extends keyof Object_>(o: Object_, key: Key) {
|
|
235
|
+
return o[key] as Get<Object_, Key>;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const symbolLiteral = Symbol('literal');
|
|
239
|
+
let symbolValue = Symbol('value1');
|
|
240
|
+
symbolValue = Symbol('value2');
|
|
241
|
+
|
|
242
|
+
get({[symbolLiteral]: 1} as const, symbolLiteral);
|
|
243
|
+
//=> 1
|
|
244
|
+
|
|
245
|
+
get({[symbolValue]: 1} as const, symbolValue);
|
|
246
|
+
//=> number
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
@category Type Guard
|
|
250
|
+
@category Utilities
|
|
251
|
+
*/
|
|
252
|
+
export type IsSymbolLiteral<T> = LiteralCheck<T, symbol>;
|
|
253
|
+
|
|
254
|
+
/** Helper type for `IsLiteral`. */
|
|
255
|
+
type IsLiteralUnion<T> =
|
|
256
|
+
| IsStringLiteral<T>
|
|
257
|
+
| IsNumericLiteral<T>
|
|
258
|
+
| IsBooleanLiteral<T>
|
|
259
|
+
| IsSymbolLiteral<T>;
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
Returns a boolean for whether the given type is a [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types).
|
|
263
|
+
|
|
264
|
+
Useful for:
|
|
265
|
+
- providing strongly-typed functions when given literal arguments
|
|
266
|
+
- type utilities, such as when constructing parsers and ASTs
|
|
267
|
+
|
|
268
|
+
@example
|
|
269
|
+
```
|
|
270
|
+
import type {IsLiteral} from 'type-fest';
|
|
271
|
+
|
|
272
|
+
type A = IsLiteral<1>;
|
|
273
|
+
//=> true
|
|
274
|
+
|
|
275
|
+
type B = IsLiteral<number>;
|
|
276
|
+
//=> false
|
|
277
|
+
|
|
278
|
+
type C = IsLiteral<1n>;
|
|
279
|
+
//=> true
|
|
280
|
+
|
|
281
|
+
type D = IsLiteral<bigint>;
|
|
282
|
+
//=> false
|
|
283
|
+
|
|
284
|
+
type E = IsLiteral<'type-fest'>;
|
|
285
|
+
//=> true
|
|
286
|
+
|
|
287
|
+
type F = IsLiteral<string>;
|
|
288
|
+
//=> false
|
|
289
|
+
|
|
290
|
+
type G = IsLiteral<`on${string}`>;
|
|
291
|
+
//=> false
|
|
292
|
+
|
|
293
|
+
declare const symbolLiteral: unique symbol;
|
|
294
|
+
type H = IsLiteral<typeof symbolLiteral>;
|
|
295
|
+
//=> true
|
|
296
|
+
|
|
297
|
+
type I = IsLiteral<symbol>;
|
|
298
|
+
//=> false
|
|
299
|
+
|
|
300
|
+
type J = IsLiteral<true>;
|
|
301
|
+
//=> true
|
|
302
|
+
|
|
303
|
+
type K = IsLiteral<boolean>;
|
|
304
|
+
//=> false
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
@category Type Guard
|
|
308
|
+
@category Utilities
|
|
309
|
+
*/
|
|
310
|
+
export type IsLiteral<T> =
|
|
311
|
+
IsPrimitive<T> extends true
|
|
312
|
+
? IsNotFalse<IsLiteralUnion<T>>
|
|
313
|
+
: false;
|
|
314
|
+
|
|
315
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type {AllExtend} from './all-extend.d.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Returns a boolean for whether the given string literal is lowercase.
|
|
5
|
+
|
|
6
|
+
@example
|
|
7
|
+
```
|
|
8
|
+
import type {IsLowercase} from 'type-fest';
|
|
9
|
+
|
|
10
|
+
type A = IsLowercase<'abc'>;
|
|
11
|
+
//=> true
|
|
12
|
+
|
|
13
|
+
type B = IsLowercase<'Abc'>;
|
|
14
|
+
//=> false
|
|
15
|
+
|
|
16
|
+
type C = IsLowercase<string>;
|
|
17
|
+
//=> boolean
|
|
18
|
+
```
|
|
19
|
+
*/
|
|
20
|
+
export type IsLowercase<S extends string> = AllExtend<_IsLowercase<S>, true>;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
Loops through each part in the string and returns a boolean array indicating whether each part is lowercase.
|
|
24
|
+
*/
|
|
25
|
+
type _IsLowercase<S extends string, Accumulator extends boolean[] = []> = S extends `${infer First}${infer Rest}`
|
|
26
|
+
? _IsLowercase<Rest, [...Accumulator, IsLowercaseHelper<First>]>
|
|
27
|
+
: [...Accumulator, IsLowercaseHelper<S>];
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
Returns a boolean for whether an individual part of the string is lowercase.
|
|
31
|
+
*/
|
|
32
|
+
type IsLowercaseHelper<S extends string> = S extends Lowercase<string>
|
|
33
|
+
? true
|
|
34
|
+
: S extends Uppercase<string> | Capitalize<string> | `${string}${Uppercase<string>}${string}`
|
|
35
|
+
? false
|
|
36
|
+
: boolean;
|
|
37
|
+
|
|
38
|
+
export {};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Returns a boolean for whether the given type is `never`.
|
|
3
|
+
|
|
4
|
+
@link https://github.com/microsoft/TypeScript/issues/31751#issuecomment-498526919
|
|
5
|
+
@link https://stackoverflow.com/a/53984913/10292952
|
|
6
|
+
@link https://www.zhenghao.io/posts/ts-never
|
|
7
|
+
|
|
8
|
+
Useful in type utilities, such as checking if something does not occur.
|
|
9
|
+
|
|
10
|
+
@example
|
|
11
|
+
```
|
|
12
|
+
import type {IsNever, And} from 'type-fest';
|
|
13
|
+
|
|
14
|
+
type A = IsNever<never>;
|
|
15
|
+
//=> true
|
|
16
|
+
|
|
17
|
+
type B = IsNever<any>;
|
|
18
|
+
//=> false
|
|
19
|
+
|
|
20
|
+
type C = IsNever<unknown>;
|
|
21
|
+
//=> false
|
|
22
|
+
|
|
23
|
+
type D = IsNever<never[]>;
|
|
24
|
+
//=> false
|
|
25
|
+
|
|
26
|
+
type E = IsNever<object>;
|
|
27
|
+
//=> false
|
|
28
|
+
|
|
29
|
+
type F = IsNever<string>;
|
|
30
|
+
//=> false
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
@example
|
|
34
|
+
```
|
|
35
|
+
import type {IsNever} from 'type-fest';
|
|
36
|
+
|
|
37
|
+
type IsTrue<T> = T extends true ? true : false;
|
|
38
|
+
|
|
39
|
+
// When a distributive conditional is instantiated with `never`, the entire conditional results in `never`.
|
|
40
|
+
type A = IsTrue<never>;
|
|
41
|
+
//=> never
|
|
42
|
+
|
|
43
|
+
// If you don't want that behaviour, you can explicitly add an `IsNever` check before the distributive conditional.
|
|
44
|
+
type IsTrueFixed<T> =
|
|
45
|
+
IsNever<T> extends true ? false : T extends true ? true : false;
|
|
46
|
+
|
|
47
|
+
type B = IsTrueFixed<never>;
|
|
48
|
+
//=> false
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
@category Type Guard
|
|
52
|
+
@category Utilities
|
|
53
|
+
*/
|
|
54
|
+
export type IsNever<T> = [T] extends [never] ? true : false;
|
|
55
|
+
|
|
56
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
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;
|
|
21
|
+
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type {IsAny} from './is-any.d.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Returns a boolean for whether the given type includes `null`.
|
|
5
|
+
|
|
6
|
+
Note: The built-in `NonNullable` type removes both `null` and `undefined`, which is not accurate for the name.
|
|
7
|
+
|
|
8
|
+
@example
|
|
9
|
+
```ts
|
|
10
|
+
import type {IsNullable} from 'type-fest';
|
|
11
|
+
|
|
12
|
+
type A = IsNullable<string>;
|
|
13
|
+
//=> false
|
|
14
|
+
|
|
15
|
+
type B = IsNullable<string | null>;
|
|
16
|
+
//=> true
|
|
17
|
+
|
|
18
|
+
type C = IsNullable<string | undefined>;
|
|
19
|
+
//=> false
|
|
20
|
+
|
|
21
|
+
type D = IsNullable<string | null | undefined>;
|
|
22
|
+
//=> true
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
@category Type Guard
|
|
26
|
+
@category Utilities
|
|
27
|
+
*/
|
|
28
|
+
export type IsNullable<T> = IsAny<T> extends true ? true : Extract<T, null> extends never ? false : true;
|
|
29
|
+
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type {IsAny} from './is-any.d.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Returns a boolean for whether the given key is an optional key of type.
|
|
5
|
+
|
|
6
|
+
This is useful when writing utility types or schema validators that need to differentiate `optional` keys.
|
|
7
|
+
|
|
8
|
+
@example
|
|
9
|
+
```
|
|
10
|
+
import type {IsOptionalKeyOf} from 'type-fest';
|
|
11
|
+
|
|
12
|
+
type User = {
|
|
13
|
+
name: string;
|
|
14
|
+
surname: string;
|
|
15
|
+
|
|
16
|
+
luckyNumber?: number;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
type Admin = {
|
|
20
|
+
name: string;
|
|
21
|
+
surname?: string;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
type T1 = IsOptionalKeyOf<User, 'luckyNumber'>;
|
|
25
|
+
//=> true
|
|
26
|
+
|
|
27
|
+
type T2 = IsOptionalKeyOf<User, 'name'>;
|
|
28
|
+
//=> false
|
|
29
|
+
|
|
30
|
+
type T3 = IsOptionalKeyOf<User, 'name' | 'luckyNumber'>;
|
|
31
|
+
//=> boolean
|
|
32
|
+
|
|
33
|
+
type T4 = IsOptionalKeyOf<User | Admin, 'name'>;
|
|
34
|
+
//=> false
|
|
35
|
+
|
|
36
|
+
type T5 = IsOptionalKeyOf<User | Admin, 'surname'>;
|
|
37
|
+
//=> boolean
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
@category Type Guard
|
|
41
|
+
@category Utilities
|
|
42
|
+
*/
|
|
43
|
+
export type IsOptionalKeyOf<Type extends object, Key extends keyof Type> =
|
|
44
|
+
IsAny<Type | Key> extends true ? never
|
|
45
|
+
: Key extends keyof Type
|
|
46
|
+
? Type extends Record<Key, Type[Key]>
|
|
47
|
+
? false
|
|
48
|
+
: true
|
|
49
|
+
: false;
|
|
50
|
+
|
|
51
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type {IsAny} from './is-any.d.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Returns a boolean for whether the given type includes `undefined`.
|
|
5
|
+
|
|
6
|
+
@example
|
|
7
|
+
```ts
|
|
8
|
+
import type {IsOptional} from 'type-fest';
|
|
9
|
+
|
|
10
|
+
type A = IsOptional<string>;
|
|
11
|
+
//=> false
|
|
12
|
+
|
|
13
|
+
type B = IsOptional<string | undefined>;
|
|
14
|
+
//=> true
|
|
15
|
+
|
|
16
|
+
type C = IsOptional<string | null>;
|
|
17
|
+
//=> false
|
|
18
|
+
|
|
19
|
+
type D = IsOptional<string | null | undefined>;
|
|
20
|
+
//=> true
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
@category Type Guard
|
|
24
|
+
@category Utilities
|
|
25
|
+
*/
|
|
26
|
+
export type IsOptional<T> = IsAny<T> extends true ? true : Extract<T, undefined> extends never ? false : true;
|
|
27
|
+
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type {IsEqual} from './is-equal.d.ts';
|
|
2
|
+
import type {IsAny} from './is-any.d.ts';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
Returns a boolean for whether the given key is a readonly key of type.
|
|
6
|
+
|
|
7
|
+
This is useful when writing utility types or schema validators that need to differentiate `readonly` keys.
|
|
8
|
+
|
|
9
|
+
@example
|
|
10
|
+
```
|
|
11
|
+
import type {IsReadonlyKeyOf} from 'type-fest';
|
|
12
|
+
|
|
13
|
+
type User = {
|
|
14
|
+
name: string;
|
|
15
|
+
surname: string;
|
|
16
|
+
|
|
17
|
+
readonly id: number;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
type Admin = {
|
|
21
|
+
name: string;
|
|
22
|
+
id: string;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
type T1 = IsReadonlyKeyOf<User, 'id'>;
|
|
26
|
+
//=> true
|
|
27
|
+
|
|
28
|
+
type T2 = IsReadonlyKeyOf<User, 'name'>;
|
|
29
|
+
//=> false
|
|
30
|
+
|
|
31
|
+
type T3 = IsReadonlyKeyOf<User, 'name' | 'id'>;
|
|
32
|
+
//=> boolean
|
|
33
|
+
|
|
34
|
+
type T4 = IsReadonlyKeyOf<User | Admin, 'name'>;
|
|
35
|
+
//=> false
|
|
36
|
+
|
|
37
|
+
type T5 = IsReadonlyKeyOf<User | Admin, 'id'>;
|
|
38
|
+
//=> boolean
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
@category Type Guard
|
|
42
|
+
@category Utilities
|
|
43
|
+
*/
|
|
44
|
+
export type IsReadonlyKeyOf<Type extends object, Key extends keyof Type> =
|
|
45
|
+
IsAny<Type | Key> extends true ? never
|
|
46
|
+
: Key extends unknown // For distributing `Key`
|
|
47
|
+
? Type extends unknown // For distributing `Type`
|
|
48
|
+
? IsEqual<
|
|
49
|
+
{[K in Key]: Type[Key]},
|
|
50
|
+
{readonly [K in Key]: Type[Key]}
|
|
51
|
+
>
|
|
52
|
+
: never // Should never happen
|
|
53
|
+
: never; // Should never happen
|
|
54
|
+
|
|
55
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type {IsOptionalKeyOf} from './is-optional-key-of.d.ts';
|
|
2
|
+
import type {Not} from './internal/type.d.ts';
|
|
3
|
+
import type {IsAny} from './is-any.d.ts';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
Returns a boolean for whether the given key is a required key of type.
|
|
7
|
+
|
|
8
|
+
This is useful when writing utility types or schema validators that need to differentiate `required` keys.
|
|
9
|
+
|
|
10
|
+
@example
|
|
11
|
+
```
|
|
12
|
+
import type {IsRequiredKeyOf} from 'type-fest';
|
|
13
|
+
|
|
14
|
+
type User = {
|
|
15
|
+
name: string;
|
|
16
|
+
surname: string;
|
|
17
|
+
|
|
18
|
+
luckyNumber?: number;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
type Admin = {
|
|
22
|
+
name: string;
|
|
23
|
+
surname?: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
type T1 = IsRequiredKeyOf<User, 'name'>;
|
|
27
|
+
//=> true
|
|
28
|
+
|
|
29
|
+
type T2 = IsRequiredKeyOf<User, 'luckyNumber'>;
|
|
30
|
+
//=> false
|
|
31
|
+
|
|
32
|
+
type T3 = IsRequiredKeyOf<User, 'name' | 'luckyNumber'>;
|
|
33
|
+
//=> boolean
|
|
34
|
+
|
|
35
|
+
type T4 = IsRequiredKeyOf<User | Admin, 'name'>;
|
|
36
|
+
//=> true
|
|
37
|
+
|
|
38
|
+
type T5 = IsRequiredKeyOf<User | Admin, 'surname'>;
|
|
39
|
+
//=> boolean
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
@category Type Guard
|
|
43
|
+
@category Utilities
|
|
44
|
+
*/
|
|
45
|
+
export type IsRequiredKeyOf<Type extends object, Key extends keyof Type> =
|
|
46
|
+
IsAny<Type | Key> extends true ? never
|
|
47
|
+
: Key extends keyof Type
|
|
48
|
+
? Not<IsOptionalKeyOf<Type, Key>>
|
|
49
|
+
: false;
|
|
50
|
+
|
|
51
|
+
export {};
|