@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,164 @@
|
|
|
1
|
+
import type {If} from '../if.d.ts';
|
|
2
|
+
import type {IsAny} from '../is-any.d.ts';
|
|
3
|
+
import type {IsNever} from '../is-never.d.ts';
|
|
4
|
+
import type {Primitive} from '../primitive.d.ts';
|
|
5
|
+
import type {UnknownArray} from '../unknown-array.d.ts';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
Matches any primitive, `void`, `Date`, or `RegExp` value.
|
|
9
|
+
*/
|
|
10
|
+
export type BuiltIns = Primitive | void | Date | RegExp;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
Matches non-recursive types.
|
|
14
|
+
*/
|
|
15
|
+
export type NonRecursiveType = BuiltIns | Function | (new (...arguments_: any[]) => unknown) | Promise<unknown>;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
Matches maps, sets, or arrays.
|
|
19
|
+
*/
|
|
20
|
+
export type MapsSetsOrArrays = ReadonlyMap<unknown, unknown> | WeakMap<WeakKey, unknown> | ReadonlySet<unknown> | WeakSet<WeakKey> | UnknownArray;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
Returns a boolean for whether the two given types extends the base type.
|
|
24
|
+
*/
|
|
25
|
+
export type IsBothExtends<BaseType, FirstType, SecondType> = FirstType extends BaseType
|
|
26
|
+
? SecondType extends BaseType
|
|
27
|
+
? true
|
|
28
|
+
: false
|
|
29
|
+
: false;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
Test if the given function has multiple call signatures.
|
|
33
|
+
|
|
34
|
+
Needed to handle the case of a single call signature with properties.
|
|
35
|
+
|
|
36
|
+
Multiple call signatures cannot currently be supported due to a TypeScript limitation.
|
|
37
|
+
@see https://github.com/microsoft/TypeScript/issues/29732
|
|
38
|
+
*/
|
|
39
|
+
export type HasMultipleCallSignatures<T extends (...arguments_: any[]) => unknown> =
|
|
40
|
+
T extends {(...arguments_: infer A): unknown; (...arguments_: infer B): unknown}
|
|
41
|
+
? B extends A
|
|
42
|
+
? A extends B
|
|
43
|
+
? false
|
|
44
|
+
: true
|
|
45
|
+
: true
|
|
46
|
+
: false;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
Returns a boolean for whether the given `boolean` is not `false`.
|
|
50
|
+
*/
|
|
51
|
+
export type IsNotFalse<T extends boolean> = [T] extends [false] ? false : true;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
Returns a boolean for whether the given type is primitive value or primitive type.
|
|
55
|
+
|
|
56
|
+
@example
|
|
57
|
+
```
|
|
58
|
+
type A = IsPrimitive<'string'>;
|
|
59
|
+
//=> true
|
|
60
|
+
|
|
61
|
+
type B = IsPrimitive<string>;
|
|
62
|
+
//=> true
|
|
63
|
+
|
|
64
|
+
type C = IsPrimitive<Object>;
|
|
65
|
+
//=> false
|
|
66
|
+
```
|
|
67
|
+
*/
|
|
68
|
+
export type IsPrimitive<T> = [T] extends [Primitive] ? true : false;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
Returns a boolean for whether A is false.
|
|
72
|
+
|
|
73
|
+
@example
|
|
74
|
+
```
|
|
75
|
+
type A = Not<true>;
|
|
76
|
+
//=> false
|
|
77
|
+
|
|
78
|
+
type B = Not<false>;
|
|
79
|
+
//=> true
|
|
80
|
+
```
|
|
81
|
+
*/
|
|
82
|
+
export type Not<A extends boolean> = A extends true
|
|
83
|
+
? false
|
|
84
|
+
: A extends false
|
|
85
|
+
? true
|
|
86
|
+
: never;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
An if-else-like type that resolves depending on whether the given type is `any` or `never`.
|
|
90
|
+
|
|
91
|
+
@example
|
|
92
|
+
```
|
|
93
|
+
// When `T` is a NOT `any` or `never` (like `string`) => Returns `IfNotAnyOrNever` branch
|
|
94
|
+
type A = IfNotAnyOrNever<string, 'VALID', 'IS_ANY', 'IS_NEVER'>;
|
|
95
|
+
//=> 'VALID'
|
|
96
|
+
|
|
97
|
+
// When `T` is `any` => Returns `IfAny` branch
|
|
98
|
+
type B = IfNotAnyOrNever<any, 'VALID', 'IS_ANY', 'IS_NEVER'>;
|
|
99
|
+
//=> 'IS_ANY'
|
|
100
|
+
|
|
101
|
+
// When `T` is `never` => Returns `IfNever` branch
|
|
102
|
+
type C = IfNotAnyOrNever<never, 'VALID', 'IS_ANY', 'IS_NEVER'>;
|
|
103
|
+
//=> 'IS_NEVER'
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Note: Wrapping a tail-recursive type with `IfNotAnyOrNever` makes the implementation non-tail-recursive. To fix this, move the recursion into a helper type. Refer to the following example:
|
|
107
|
+
|
|
108
|
+
@example
|
|
109
|
+
```ts
|
|
110
|
+
import type {StringRepeat} from 'type-fest';
|
|
111
|
+
|
|
112
|
+
type NineHundredNinetyNineSpaces = StringRepeat<' ', 999>;
|
|
113
|
+
|
|
114
|
+
// The following implementation is not tail recursive
|
|
115
|
+
type TrimLeft<S extends string> = IfNotAnyOrNever<S, S extends ` ${infer R}` ? TrimLeft<R> : S>;
|
|
116
|
+
|
|
117
|
+
// Hence, instantiations with long strings will fail
|
|
118
|
+
// @ts-expect-error
|
|
119
|
+
type T1 = TrimLeft<NineHundredNinetyNineSpaces>;
|
|
120
|
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
121
|
+
// Error: Type instantiation is excessively deep and possibly infinite.
|
|
122
|
+
|
|
123
|
+
// To fix this, move the recursion into a helper type
|
|
124
|
+
type TrimLeftOptimised<S extends string> = IfNotAnyOrNever<S, _TrimLeftOptimised<S>>;
|
|
125
|
+
|
|
126
|
+
type _TrimLeftOptimised<S extends string> = S extends ` ${infer R}` ? _TrimLeftOptimised<R> : S;
|
|
127
|
+
|
|
128
|
+
type T2 = TrimLeftOptimised<NineHundredNinetyNineSpaces>;
|
|
129
|
+
//=> ''
|
|
130
|
+
```
|
|
131
|
+
*/
|
|
132
|
+
export type IfNotAnyOrNever<T, IfNotAnyOrNever, IfAny = any, IfNever = never> =
|
|
133
|
+
If<IsAny<T>, IfAny, If<IsNever<T>, IfNever, IfNotAnyOrNever>>;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
Returns a boolean for whether the given type is `any` or `never`.
|
|
137
|
+
|
|
138
|
+
This type can be better to use than {@link IfNotAnyOrNever `IfNotAnyOrNever`} in recursive types because it does not evaluate any branches.
|
|
139
|
+
|
|
140
|
+
@example
|
|
141
|
+
```
|
|
142
|
+
// When `T` is a NOT `any` or `never` (like `string`) => Returns `false`
|
|
143
|
+
type A = IsAnyOrNever<string>;
|
|
144
|
+
//=> false
|
|
145
|
+
|
|
146
|
+
// When `T` is `any` => Returns `true`
|
|
147
|
+
type B = IsAnyOrNever<any>;
|
|
148
|
+
//=> true
|
|
149
|
+
|
|
150
|
+
// When `T` is `never` => Returns `true`
|
|
151
|
+
type C = IsAnyOrNever<never>;
|
|
152
|
+
//=> true
|
|
153
|
+
```
|
|
154
|
+
*/
|
|
155
|
+
export type IsAnyOrNever<T> = IsNotFalse<IsAny<T> | IsNever<T>>;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
Indicates the value of `exactOptionalPropertyTypes` compiler option.
|
|
159
|
+
*/
|
|
160
|
+
export type IsExactOptionalPropertyTypesEnabled = [(string | undefined)?] extends [string?]
|
|
161
|
+
? false
|
|
162
|
+
: true;
|
|
163
|
+
|
|
164
|
+
export {};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
declare const invariantBrand: unique symbol;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
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.
|
|
5
|
+
|
|
6
|
+
Use-case:
|
|
7
|
+
- Prevent runtime errors that may occur due to assigning subtypes to supertypes.
|
|
8
|
+
- Improve type signature of object methods like [`Object.keys()` or `Object.entries()`](https://github.com/microsoft/TypeScript/pull/12253#issuecomment-263132208) by sealing the object type.
|
|
9
|
+
|
|
10
|
+
@example
|
|
11
|
+
```
|
|
12
|
+
import type {InvariantOf} from 'type-fest';
|
|
13
|
+
|
|
14
|
+
class Animal {
|
|
15
|
+
constructor(public name: string) {}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
class Cat extends Animal {
|
|
19
|
+
meow() {
|
|
20
|
+
// do something
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
let animalArray: Animal[] = [new Animal('jerry')];
|
|
25
|
+
const catArray: Cat[] = [new Cat('tom')];
|
|
26
|
+
|
|
27
|
+
animalArray = catArray; // Okay if covariant
|
|
28
|
+
animalArray.push(new Animal('another animal')); // Pushed an animal into catArray
|
|
29
|
+
for (const c of catArray) {
|
|
30
|
+
c.meow();
|
|
31
|
+
} // Allowed but, error at runtime
|
|
32
|
+
|
|
33
|
+
let invariantAnimalArray: Array<InvariantOf<Animal>> = [new Animal('jerry')] as Array<InvariantOf<Animal>>;
|
|
34
|
+
const invariantCatArray: Array<InvariantOf<Cat>> = [new Cat('tom')] as Array<InvariantOf<Cat>>;
|
|
35
|
+
|
|
36
|
+
// @ts-expect-error
|
|
37
|
+
invariantAnimalArray = invariantCatArray; // Error: Type 'InvariantOf<Cat>[]' is not assignable to type 'InvariantOf<Animal>[]'.
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
@example
|
|
41
|
+
```
|
|
42
|
+
import type {InvariantOf} from 'type-fest';
|
|
43
|
+
|
|
44
|
+
// In covariance (default)
|
|
45
|
+
|
|
46
|
+
type FooBar = {
|
|
47
|
+
foo: number;
|
|
48
|
+
bar: string;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
type FooBarBaz = {
|
|
52
|
+
baz: boolean;
|
|
53
|
+
} & FooBar;
|
|
54
|
+
|
|
55
|
+
declare const fooBar: FooBar;
|
|
56
|
+
declare const fooBarBaz: FooBarBaz;
|
|
57
|
+
|
|
58
|
+
function keyOfFooBar(fooBar: FooBar) {
|
|
59
|
+
return Object.keys(fooBar) as Array<keyof FooBar>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
keyOfFooBar(fooBar); //=> (keyof FooBar)[]
|
|
63
|
+
keyOfFooBar(fooBarBaz); //=> (keyof FooBar)[] but, (keyof FooBarBaz)[] at runtime
|
|
64
|
+
|
|
65
|
+
// In invariance
|
|
66
|
+
|
|
67
|
+
export function invariantOf<Type>(value: Type): InvariantOf<Type> {
|
|
68
|
+
return value as InvariantOf<Type>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function keyOfInvariantFooBar(fooBar: InvariantOf<FooBar>) {
|
|
72
|
+
return Object.keys(fooBar) as Array<keyof FooBar>;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
keyOfInvariantFooBar(invariantOf(fooBar)); // (keyof FooBar)[]
|
|
76
|
+
|
|
77
|
+
// @ts-expect-error
|
|
78
|
+
keyOfInvariantFooBar(invariantOf(fooBarBaz)); // Error: Argument of type 'InvariantOf<FooBarBaz>' is not assignable to parameter of type 'InvariantOf<FooBar>'.
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
@category Type
|
|
82
|
+
*/
|
|
83
|
+
export type InvariantOf<Type> = Type & {[invariantBrand]: (_: Type) => Type};
|
|
84
|
+
|
|
85
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Returns a boolean for whether the given type is `any`.
|
|
3
|
+
|
|
4
|
+
@link https://stackoverflow.com/a/49928360/1490091
|
|
5
|
+
|
|
6
|
+
Useful in type utilities, such as disallowing `any`s to be passed to a function.
|
|
7
|
+
|
|
8
|
+
@example
|
|
9
|
+
```
|
|
10
|
+
import type {IsAny} from 'type-fest';
|
|
11
|
+
|
|
12
|
+
const typedObject = {a: 1, b: 2} as const;
|
|
13
|
+
const anyObject: any = {a: 1, b: 2};
|
|
14
|
+
|
|
15
|
+
function get<O extends (IsAny<O> extends true ? {} : Record<string, number>), K extends keyof O = keyof O>(object: O, key: K) {
|
|
16
|
+
return object[key];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const typedA = get(typedObject, 'a');
|
|
20
|
+
//=> 1
|
|
21
|
+
|
|
22
|
+
const anyA = get(anyObject, 'a');
|
|
23
|
+
//=> any
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
@category Type Guard
|
|
27
|
+
@category Utilities
|
|
28
|
+
*/
|
|
29
|
+
export type IsAny<T> = 0 extends 1 & NoInfer<T> ? true : false;
|
|
30
|
+
|
|
31
|
+
export {};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type {IsNever} from './is-never.d.ts';
|
|
2
|
+
/**
|
|
3
|
+
Returns a boolean for whether the two given types are equal.
|
|
4
|
+
|
|
5
|
+
@link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650
|
|
6
|
+
@link https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796
|
|
7
|
+
|
|
8
|
+
Use-cases:
|
|
9
|
+
- If you want to make a conditional branch based on the result of a comparison of two types.
|
|
10
|
+
|
|
11
|
+
@example
|
|
12
|
+
```
|
|
13
|
+
import type {IsEqual} from 'type-fest';
|
|
14
|
+
|
|
15
|
+
// This type returns a boolean for whether the given array includes the given item.
|
|
16
|
+
// `IsEqual` is used to compare the given array at position 0 and the given item and then return true if they are equal.
|
|
17
|
+
type Includes<Value extends readonly any[], Item> =
|
|
18
|
+
Value extends readonly [Value[0], ...infer rest]
|
|
19
|
+
? IsEqual<Value[0], Item> extends true
|
|
20
|
+
? true
|
|
21
|
+
: Includes<rest, Item>
|
|
22
|
+
: false;
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
@category Type Guard
|
|
26
|
+
@category Utilities
|
|
27
|
+
*/
|
|
28
|
+
export type IsEqual<A, B> =
|
|
29
|
+
[A] extends [B]
|
|
30
|
+
? [B] extends [A]
|
|
31
|
+
? _IsEqual<A, B>
|
|
32
|
+
: false
|
|
33
|
+
: false;
|
|
34
|
+
|
|
35
|
+
// This version fails the `equalWrappedTupleIntersectionToBeNeverAndNeverExpanded` test in `test-d/is-equal.ts`.
|
|
36
|
+
type _IsEqual<A, B> =
|
|
37
|
+
(<G>() => G extends A & G | G ? 1 : 2) extends
|
|
38
|
+
(<G>() => G extends B & G | G ? 1 : 2)
|
|
39
|
+
? true
|
|
40
|
+
: false;
|
|
41
|
+
|
|
42
|
+
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Returns a boolean for whether the given number is a float, like `1.5` or `-1.5`.
|
|
3
|
+
|
|
4
|
+
Use-case:
|
|
5
|
+
- If you want to make a conditional branch based on the result of whether a number is a float or not.
|
|
6
|
+
|
|
7
|
+
@example
|
|
8
|
+
```
|
|
9
|
+
import type {IsFloat, PositiveInfinity} from 'type-fest';
|
|
10
|
+
|
|
11
|
+
type A = IsFloat<1.5>;
|
|
12
|
+
//=> true
|
|
13
|
+
|
|
14
|
+
type B = IsFloat<-1.5>;
|
|
15
|
+
//=> true
|
|
16
|
+
|
|
17
|
+
type C = IsFloat<1e-7>;
|
|
18
|
+
//=> true
|
|
19
|
+
|
|
20
|
+
type D = IsFloat<1.0>;
|
|
21
|
+
//=> false
|
|
22
|
+
|
|
23
|
+
type E = IsFloat<PositiveInfinity>;
|
|
24
|
+
//=> false
|
|
25
|
+
|
|
26
|
+
type F = IsFloat<1.23e+21>;
|
|
27
|
+
//=> false
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
@category Type Guard
|
|
31
|
+
@category Numeric
|
|
32
|
+
*/
|
|
33
|
+
export type IsFloat<T> = T extends number
|
|
34
|
+
? `${T}` extends `${number}e${infer E extends '-' | '+'}${number}`
|
|
35
|
+
? E extends '-'
|
|
36
|
+
? true
|
|
37
|
+
: false
|
|
38
|
+
: `${T}` extends `${number}.${number}`
|
|
39
|
+
? true
|
|
40
|
+
: false
|
|
41
|
+
: false;
|
|
42
|
+
|
|
43
|
+
export {};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type {Not} from './internal/index.d.ts';
|
|
2
|
+
import type {IsFloat} from './is-float.d.ts';
|
|
3
|
+
import type {PositiveInfinity, NegativeInfinity} from './numeric.d.ts';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
Returns a boolean for whether the given number is an integer, like `-5`, `1.0`, or `100`.
|
|
7
|
+
|
|
8
|
+
Use-case:
|
|
9
|
+
- If you want to make a conditional branch based on the result of whether a number is an integer or not.
|
|
10
|
+
|
|
11
|
+
@example
|
|
12
|
+
```
|
|
13
|
+
import type {IsInteger, PositiveInfinity} from 'type-fest';
|
|
14
|
+
|
|
15
|
+
type A = IsInteger<1>;
|
|
16
|
+
//=> true
|
|
17
|
+
|
|
18
|
+
type B = IsInteger<1.0>;
|
|
19
|
+
//=> true
|
|
20
|
+
|
|
21
|
+
type C = IsInteger<-1>;
|
|
22
|
+
//=> true
|
|
23
|
+
|
|
24
|
+
type D = IsInteger<0b10>;
|
|
25
|
+
//=> true
|
|
26
|
+
|
|
27
|
+
type E = IsInteger<0o10>;
|
|
28
|
+
//=> true
|
|
29
|
+
|
|
30
|
+
type F = IsInteger<0x10>;
|
|
31
|
+
//=> true
|
|
32
|
+
|
|
33
|
+
type G = IsInteger<1.23e+21>;
|
|
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
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
@category Type Guard
|
|
47
|
+
@category Numeric
|
|
48
|
+
*/
|
|
49
|
+
export type IsInteger<T> =
|
|
50
|
+
T extends bigint
|
|
51
|
+
? true
|
|
52
|
+
: T extends number
|
|
53
|
+
? number extends T
|
|
54
|
+
? false
|
|
55
|
+
: T extends PositiveInfinity | NegativeInfinity
|
|
56
|
+
? false
|
|
57
|
+
: Not<IsFloat<T>>
|
|
58
|
+
: false;
|
|
59
|
+
|
|
60
|
+
export {};
|