@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,78 @@
|
|
|
1
|
+
declare global {
|
|
2
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions -- It has to be an `interface` so that it can be merged.
|
|
3
|
+
interface SymbolConstructor {
|
|
4
|
+
readonly observable: symbol;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
@remarks
|
|
10
|
+
The TC39 observable proposal defines a `closed` property, but some implementations (such as xstream) do not as of 10/08/2021.
|
|
11
|
+
As well, some guidance on making an `Observable` to not include `closed` property.
|
|
12
|
+
|
|
13
|
+
@see https://github.com/tc39/proposal-observable/blob/master/src/Observable.js#L129-L130
|
|
14
|
+
@see https://github.com/staltz/xstream/blob/6c22580c1d84d69773ee4b0905df44ad464955b3/src/index.ts#L79-L85
|
|
15
|
+
@see https://github.com/benlesh/symbol-observable#making-an-object-observable
|
|
16
|
+
|
|
17
|
+
@category Observable
|
|
18
|
+
*/
|
|
19
|
+
// eslint-disable-next-line type-fest/require-exported-types
|
|
20
|
+
export type Unsubscribable = {
|
|
21
|
+
unsubscribe(): void;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
@category Observable
|
|
26
|
+
*/
|
|
27
|
+
type OnNext<ValueType> = (value: ValueType) => void;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
@category Observable
|
|
31
|
+
*/
|
|
32
|
+
type OnError = (error: unknown) => void;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
@category Observable
|
|
36
|
+
*/
|
|
37
|
+
type OnComplete = () => void;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
@category Observable
|
|
41
|
+
*/
|
|
42
|
+
// eslint-disable-next-line type-fest/require-exported-types
|
|
43
|
+
export type Observer<ValueType> = {
|
|
44
|
+
next: OnNext<ValueType>;
|
|
45
|
+
error: OnError;
|
|
46
|
+
complete: OnComplete;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
Matches a value that is like an [Observable](https://github.com/tc39/proposal-observable).
|
|
51
|
+
|
|
52
|
+
You must import it as a sub-import:
|
|
53
|
+
|
|
54
|
+
@example
|
|
55
|
+
```
|
|
56
|
+
import type {ObservableLike} from 'type-fest/globals';
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
@remarks
|
|
60
|
+
The TC39 Observable proposal defines 2 forms of `subscribe()`:
|
|
61
|
+
1. Three callback arguments: `subscribe(observer: OnNext<ValueType>, onError?: OnError, onComplete?: OnComplete): Unsubscribable;`
|
|
62
|
+
2. A single `observer` argument: (as defined below)
|
|
63
|
+
|
|
64
|
+
But `Observable` implementations have evolved to preferring case 2 and some implementations choose not to implement case 1. Therefore, an `ObservableLike` cannot be trusted to implement the first case. (xstream and hand built observerables often do not implement case 1)
|
|
65
|
+
|
|
66
|
+
@see https://github.com/tc39/proposal-observable#observable
|
|
67
|
+
@see https://github.com/tc39/proposal-observable/blob/master/src/Observable.js#L246-L259
|
|
68
|
+
@see https://benlesh.com/posts/learning-observable-by-building-observable/
|
|
69
|
+
|
|
70
|
+
@category Observable
|
|
71
|
+
*/
|
|
72
|
+
// eslint-disable-next-line type-fest/require-exported-types
|
|
73
|
+
export type ObservableLike<ValueType = unknown> = {
|
|
74
|
+
subscribe(observer?: Partial<Observer<ValueType>>): Unsubscribable;
|
|
75
|
+
[Symbol.observable](): ObservableLike<ValueType>;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type {GreaterThan} from './greater-than.d.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Returns a boolean for whether a given number is greater than or equal to another number.
|
|
5
|
+
|
|
6
|
+
@example
|
|
7
|
+
```
|
|
8
|
+
import type {GreaterThanOrEqual} from 'type-fest';
|
|
9
|
+
|
|
10
|
+
type A = GreaterThanOrEqual<1, -5>;
|
|
11
|
+
//=> true
|
|
12
|
+
|
|
13
|
+
type B = GreaterThanOrEqual<1, 1>;
|
|
14
|
+
//=> true
|
|
15
|
+
|
|
16
|
+
type C = GreaterThanOrEqual<1, 5>;
|
|
17
|
+
//=> false
|
|
18
|
+
```
|
|
19
|
+
*/
|
|
20
|
+
export type GreaterThanOrEqual<A extends number, B extends number> = number extends A | B
|
|
21
|
+
? never
|
|
22
|
+
: A extends number // For distributing `A`
|
|
23
|
+
? B extends number // For distributing `B`
|
|
24
|
+
? A extends B
|
|
25
|
+
? true
|
|
26
|
+
: GreaterThan<A, B>
|
|
27
|
+
: never // Should never happen
|
|
28
|
+
: never; // Should never happen
|
|
29
|
+
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type {NumberAbsolute, PositiveNumericStringGt} from './internal/index.d.ts';
|
|
2
|
+
import type {IsEqual} from './is-equal.d.ts';
|
|
3
|
+
import type {PositiveInfinity, NegativeInfinity, IsNegative} from './numeric.d.ts';
|
|
4
|
+
import type {And} from './and.d.ts';
|
|
5
|
+
import type {Or} from './or.d.ts';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
Returns a boolean for whether a given number is greater than another number.
|
|
9
|
+
|
|
10
|
+
@example
|
|
11
|
+
```
|
|
12
|
+
import type {GreaterThan} from 'type-fest';
|
|
13
|
+
|
|
14
|
+
type A = GreaterThan<1, -5>;
|
|
15
|
+
//=> true
|
|
16
|
+
|
|
17
|
+
type B = GreaterThan<1, 1>;
|
|
18
|
+
//=> false
|
|
19
|
+
|
|
20
|
+
type C = GreaterThan<1, 5>;
|
|
21
|
+
//=> false
|
|
22
|
+
```
|
|
23
|
+
*/
|
|
24
|
+
export type GreaterThan<A extends number, B extends number> =
|
|
25
|
+
A extends number // For distributing `A`
|
|
26
|
+
? B extends number // For distributing `B`
|
|
27
|
+
? number extends A | B
|
|
28
|
+
? never
|
|
29
|
+
: [
|
|
30
|
+
IsEqual<A, PositiveInfinity>, IsEqual<A, NegativeInfinity>,
|
|
31
|
+
IsEqual<B, PositiveInfinity>, IsEqual<B, NegativeInfinity>,
|
|
32
|
+
] extends infer R extends [boolean, boolean, boolean, boolean]
|
|
33
|
+
? Or<
|
|
34
|
+
And<IsEqual<R[0], true>, IsEqual<R[2], false>>,
|
|
35
|
+
And<IsEqual<R[3], true>, IsEqual<R[1], false>>
|
|
36
|
+
> extends true
|
|
37
|
+
? true
|
|
38
|
+
: Or<
|
|
39
|
+
And<IsEqual<R[1], true>, IsEqual<R[3], false>>,
|
|
40
|
+
And<IsEqual<R[2], true>, IsEqual<R[0], false>>
|
|
41
|
+
> extends true
|
|
42
|
+
? false
|
|
43
|
+
: true extends R[number]
|
|
44
|
+
? false
|
|
45
|
+
: [IsNegative<A>, IsNegative<B>] extends infer R extends [boolean, boolean]
|
|
46
|
+
? [true, false] extends R
|
|
47
|
+
? false
|
|
48
|
+
: [false, true] extends R
|
|
49
|
+
? true
|
|
50
|
+
: [false, false] extends R
|
|
51
|
+
? PositiveNumericStringGt<`${A}`, `${B}`>
|
|
52
|
+
: PositiveNumericStringGt<`${NumberAbsolute<B>}`, `${NumberAbsolute<A>}`>
|
|
53
|
+
: never
|
|
54
|
+
: never
|
|
55
|
+
: never // Should never happen
|
|
56
|
+
: never; // Should never happen
|
|
57
|
+
|
|
58
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type {OptionalKeysOf} from './optional-keys-of.d.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Creates a type that represents `true` or `false` depending on whether the given type has any optional fields.
|
|
5
|
+
|
|
6
|
+
This is useful when you want to create an API whose behavior depends on the presence or absence of optional fields.
|
|
7
|
+
|
|
8
|
+
@example
|
|
9
|
+
```
|
|
10
|
+
import type {HasOptionalKeys, OptionalKeysOf} from 'type-fest';
|
|
11
|
+
|
|
12
|
+
type UpdateService<Entity extends object> = {
|
|
13
|
+
removeField: HasOptionalKeys<Entity> extends true
|
|
14
|
+
? (field: OptionalKeysOf<Entity>) => Promise<void>
|
|
15
|
+
: never;
|
|
16
|
+
};
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
@category Utilities
|
|
20
|
+
*/
|
|
21
|
+
export type HasOptionalKeys<BaseType extends object> = OptionalKeysOf<BaseType> extends never ? false : true;
|
|
22
|
+
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type {ReadonlyKeysOf} from './readonly-keys-of.d.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Creates a type that represents `true` or `false` depending on whether the given type has any readonly fields.
|
|
5
|
+
|
|
6
|
+
This is useful when you want to create an API whose behavior depends on the presence or absence of readonly fields.
|
|
7
|
+
|
|
8
|
+
@example
|
|
9
|
+
```
|
|
10
|
+
import type {HasReadonlyKeys, ReadonlyKeysOf} from 'type-fest';
|
|
11
|
+
|
|
12
|
+
type UpdateService<Entity extends object> = {
|
|
13
|
+
removeField: HasReadonlyKeys<Entity> extends true
|
|
14
|
+
? (field: ReadonlyKeysOf<Entity>) => Promise<void>
|
|
15
|
+
: never;
|
|
16
|
+
};
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
@category Utilities
|
|
20
|
+
*/
|
|
21
|
+
export type HasReadonlyKeys<BaseType extends object> = ReadonlyKeysOf<BaseType> extends never ? false : true;
|
|
22
|
+
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type {RequiredKeysOf} from './required-keys-of.d.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Creates a type that represents `true` or `false` depending on whether the given type has any required fields.
|
|
5
|
+
|
|
6
|
+
This is useful when you want to create an API whose behavior depends on the presence or absence of required fields.
|
|
7
|
+
|
|
8
|
+
@example
|
|
9
|
+
```
|
|
10
|
+
import type {HasRequiredKeys} from 'type-fest';
|
|
11
|
+
|
|
12
|
+
type GeneratorOptions<Template extends object> = {
|
|
13
|
+
prop1: number;
|
|
14
|
+
prop2: string;
|
|
15
|
+
} & (HasRequiredKeys<Template> extends true
|
|
16
|
+
? {template: Template}
|
|
17
|
+
: {template?: Template});
|
|
18
|
+
|
|
19
|
+
type Template1 = {
|
|
20
|
+
optionalSubParam?: string;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
type Template2 = {
|
|
24
|
+
requiredSubParam: string;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
type Options1 = GeneratorOptions<Template1>;
|
|
28
|
+
type Options2 = GeneratorOptions<Template2>;
|
|
29
|
+
|
|
30
|
+
const optA: Options1 = {
|
|
31
|
+
prop1: 0,
|
|
32
|
+
prop2: 'hi',
|
|
33
|
+
};
|
|
34
|
+
const optB: Options1 = {
|
|
35
|
+
prop1: 0,
|
|
36
|
+
prop2: 'hi',
|
|
37
|
+
template: {},
|
|
38
|
+
};
|
|
39
|
+
const optC: Options1 = {
|
|
40
|
+
prop1: 0,
|
|
41
|
+
prop2: 'hi',
|
|
42
|
+
template: {
|
|
43
|
+
optionalSubParam: 'optional value',
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const optD: Options2 = {
|
|
48
|
+
prop1: 0,
|
|
49
|
+
prop2: 'hi',
|
|
50
|
+
template: {
|
|
51
|
+
requiredSubParam: 'required value',
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
@category Utilities
|
|
58
|
+
*/
|
|
59
|
+
export type HasRequiredKeys<BaseType extends object> = RequiredKeysOf<BaseType> extends never ? false : true;
|
|
60
|
+
|
|
61
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type {WritableKeysOf} from './writable-keys-of.d.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Creates a type that represents `true` or `false` depending on whether the given type has any writable fields.
|
|
5
|
+
|
|
6
|
+
This is useful when you want to create an API whose behavior depends on the presence or absence of writable fields.
|
|
7
|
+
|
|
8
|
+
@example
|
|
9
|
+
```
|
|
10
|
+
import type {HasWritableKeys, WritableKeysOf} from 'type-fest';
|
|
11
|
+
|
|
12
|
+
type UpdateService<Entity extends object> = {
|
|
13
|
+
removeField: HasWritableKeys<Entity> extends true
|
|
14
|
+
? (field: WritableKeysOf<Entity>) => Promise<void>
|
|
15
|
+
: never;
|
|
16
|
+
};
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
@category Utilities
|
|
20
|
+
*/
|
|
21
|
+
export type HasWritableKeys<BaseType extends object> = WritableKeysOf<BaseType> extends never ? false : true;
|
|
22
|
+
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type {IsAny} from './is-any.d.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
An if-else-like type that resolves depending on whether the given type is `any`.
|
|
5
|
+
|
|
6
|
+
@deprecated This type will be removed in the next major version. Use the {@link If} type instead.
|
|
7
|
+
|
|
8
|
+
@see {@link IsAny}
|
|
9
|
+
|
|
10
|
+
@example
|
|
11
|
+
```
|
|
12
|
+
import type {IfAny} from 'type-fest';
|
|
13
|
+
|
|
14
|
+
type ShouldBeTrue = IfAny<any>;
|
|
15
|
+
//=> true
|
|
16
|
+
|
|
17
|
+
type ShouldBeBar = IfAny<'not any', 'foo', 'bar'>;
|
|
18
|
+
//=> 'bar'
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
@category Type Guard
|
|
22
|
+
@category Utilities
|
|
23
|
+
*/
|
|
24
|
+
export type IfAny<T, TypeIfAny = true, TypeIfNotAny = false> = (
|
|
25
|
+
IsAny<T> extends true ? TypeIfAny : TypeIfNotAny
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type {IsEmptyObject} from './empty-object.d.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
An if-else-like type that resolves depending on whether the given type is `{}`.
|
|
5
|
+
|
|
6
|
+
@deprecated This type will be removed in the next major version. Use the {@link If} type instead.
|
|
7
|
+
|
|
8
|
+
@see {@link IsEmptyObject}
|
|
9
|
+
|
|
10
|
+
@example
|
|
11
|
+
```
|
|
12
|
+
import type {IfEmptyObject} from 'type-fest';
|
|
13
|
+
|
|
14
|
+
type ShouldBeTrue = IfEmptyObject<{}>;
|
|
15
|
+
//=> true
|
|
16
|
+
|
|
17
|
+
type ShouldBeBar = IfEmptyObject<{key: any}, 'foo', 'bar'>;
|
|
18
|
+
//=> 'bar'
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
@category Type Guard
|
|
22
|
+
@category Utilities
|
|
23
|
+
*/
|
|
24
|
+
export type IfEmptyObject<
|
|
25
|
+
T,
|
|
26
|
+
TypeIfEmptyObject = true,
|
|
27
|
+
TypeIfNotEmptyObject = false,
|
|
28
|
+
> = IsEmptyObject<T> extends true ? TypeIfEmptyObject : TypeIfNotEmptyObject;
|
|
29
|
+
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type {IsNever} from './is-never.d.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
An if-else-like type that resolves depending on whether the given type is `never`.
|
|
5
|
+
|
|
6
|
+
@deprecated This type will be removed in the next major version. Use the {@link If} type instead.
|
|
7
|
+
|
|
8
|
+
@see {@link IsNever}
|
|
9
|
+
|
|
10
|
+
@example
|
|
11
|
+
```
|
|
12
|
+
import type {IfNever} from 'type-fest';
|
|
13
|
+
|
|
14
|
+
type ShouldBeTrue = IfNever<never>;
|
|
15
|
+
//=> true
|
|
16
|
+
|
|
17
|
+
type ShouldBeBar = IfNever<'not never', 'foo', 'bar'>;
|
|
18
|
+
//=> 'bar'
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
@category Type Guard
|
|
22
|
+
@category Utilities
|
|
23
|
+
*/
|
|
24
|
+
export type IfNever<T, TypeIfNever = true, TypeIfNotNever = false> = (
|
|
25
|
+
IsNever<T> extends true ? TypeIfNever : TypeIfNotNever
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type {IsNull} from './is-null.d.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
An if-else-like type that resolves depending on whether the given type is `null`.
|
|
5
|
+
|
|
6
|
+
@deprecated This type will be removed in the next major version. Use the {@link If} type instead.
|
|
7
|
+
|
|
8
|
+
@see {@link IsNull}
|
|
9
|
+
|
|
10
|
+
@example
|
|
11
|
+
```
|
|
12
|
+
import type {IfNull} from 'type-fest';
|
|
13
|
+
|
|
14
|
+
type ShouldBeTrue = IfNull<null>;
|
|
15
|
+
//=> true
|
|
16
|
+
|
|
17
|
+
type ShouldBeBar = IfNull<'not null', 'foo', 'bar'>;
|
|
18
|
+
//=> 'bar'
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
@category Type Guard
|
|
22
|
+
@category Utilities
|
|
23
|
+
*/
|
|
24
|
+
export type IfNull<T, TypeIfNull = true, TypeIfNotNull = false> = (
|
|
25
|
+
IsNull<T> extends true ? TypeIfNull : TypeIfNotNull
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type {IsUnknown} from './is-unknown.d.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
An if-else-like type that resolves depending on whether the given type is `unknown`.
|
|
5
|
+
|
|
6
|
+
@deprecated This type will be removed in the next major version. Use the {@link If} type instead.
|
|
7
|
+
|
|
8
|
+
@see {@link IsUnknown}
|
|
9
|
+
|
|
10
|
+
@example
|
|
11
|
+
```
|
|
12
|
+
import type {IfUnknown} from 'type-fest';
|
|
13
|
+
|
|
14
|
+
type ShouldBeTrue = IfUnknown<unknown>;
|
|
15
|
+
//=> true
|
|
16
|
+
|
|
17
|
+
type ShouldBeBar = IfUnknown<'not unknown', 'foo', 'bar'>;
|
|
18
|
+
//=> 'bar'
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
@category Type Guard
|
|
22
|
+
@category Utilities
|
|
23
|
+
*/
|
|
24
|
+
export type IfUnknown<T, TypeIfUnknown = true, TypeIfNotUnknown = false> = (
|
|
25
|
+
IsUnknown<T> extends true ? TypeIfUnknown : TypeIfNotUnknown
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
export {};
|
package/source/if.d.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type {IsNever} from './is-never.d.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
An if-else-like type that resolves depending on whether the given `boolean` type is `true` or `false`.
|
|
5
|
+
|
|
6
|
+
Use-cases:
|
|
7
|
+
- You can use this in combination with `Is*` types to create an if-else-like experience. For example, `If<IsAny<any>, 'is any', 'not any'>`.
|
|
8
|
+
|
|
9
|
+
Note:
|
|
10
|
+
- Returns a union of if branch and else branch if the given type is `boolean` or `any`. For example, `If<boolean, 'Y', 'N'>` will return `'Y' | 'N'`.
|
|
11
|
+
- Returns the else branch if the given type is `never`. For example, `If<never, 'Y', 'N'>` will return `'N'`.
|
|
12
|
+
|
|
13
|
+
@example
|
|
14
|
+
```
|
|
15
|
+
import type {If} from 'type-fest';
|
|
16
|
+
|
|
17
|
+
type A = If<true, 'yes', 'no'>;
|
|
18
|
+
//=> 'yes'
|
|
19
|
+
|
|
20
|
+
type B = If<false, 'yes', 'no'>;
|
|
21
|
+
//=> 'no'
|
|
22
|
+
|
|
23
|
+
type C = If<boolean, 'yes', 'no'>;
|
|
24
|
+
//=> 'yes' | 'no'
|
|
25
|
+
|
|
26
|
+
type D = If<any, 'yes', 'no'>;
|
|
27
|
+
//=> 'yes' | 'no'
|
|
28
|
+
|
|
29
|
+
type E = If<never, 'yes', 'no'>;
|
|
30
|
+
//=> 'no'
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
@example
|
|
34
|
+
```
|
|
35
|
+
import type {If, IsAny, IsNever} from 'type-fest';
|
|
36
|
+
|
|
37
|
+
type A = If<IsAny<unknown>, 'is any', 'not any'>;
|
|
38
|
+
//=> 'not any'
|
|
39
|
+
|
|
40
|
+
type B = If<IsNever<never>, 'is never', 'not never'>;
|
|
41
|
+
//=> 'is never'
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
@example
|
|
45
|
+
```
|
|
46
|
+
import type {If, IsEqual} from 'type-fest';
|
|
47
|
+
|
|
48
|
+
type IfEqual<T, U, IfBranch, ElseBranch> = If<IsEqual<T, U>, IfBranch, ElseBranch>;
|
|
49
|
+
|
|
50
|
+
type A = IfEqual<string, string, 'equal', 'not equal'>;
|
|
51
|
+
//=> 'equal'
|
|
52
|
+
|
|
53
|
+
type B = IfEqual<string, number, 'equal', 'not equal'>;
|
|
54
|
+
//=> 'not equal'
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Note: Sometimes using the `If` type can make an implementation non–tail-recursive, which can impact performance. In such cases, it’s better to use a conditional directly. Refer to the following example:
|
|
58
|
+
|
|
59
|
+
@example
|
|
60
|
+
```
|
|
61
|
+
import type {If, IsEqual, StringRepeat} from 'type-fest';
|
|
62
|
+
|
|
63
|
+
type HundredZeroes = StringRepeat<'0', 100>;
|
|
64
|
+
|
|
65
|
+
// The following implementation is not tail recursive
|
|
66
|
+
type Includes<S extends string, Char extends string> =
|
|
67
|
+
S extends `${infer First}${infer Rest}`
|
|
68
|
+
? If<IsEqual<First, Char>,
|
|
69
|
+
'found',
|
|
70
|
+
Includes<Rest, Char>>
|
|
71
|
+
: 'not found';
|
|
72
|
+
|
|
73
|
+
// Hence, instantiations with long strings will fail
|
|
74
|
+
// @ts-expect-error
|
|
75
|
+
type Fails = Includes<HundredZeroes, '1'>;
|
|
76
|
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
77
|
+
// Error: Type instantiation is excessively deep and possibly infinite.
|
|
78
|
+
|
|
79
|
+
// However, if we use a simple conditional instead of `If`, the implementation becomes tail-recursive
|
|
80
|
+
type IncludesWithoutIf<S extends string, Char extends string> =
|
|
81
|
+
S extends `${infer First}${infer Rest}`
|
|
82
|
+
? IsEqual<First, Char> extends true
|
|
83
|
+
? 'found'
|
|
84
|
+
: IncludesWithoutIf<Rest, Char>
|
|
85
|
+
: 'not found';
|
|
86
|
+
|
|
87
|
+
// Now, instantiations with long strings will work
|
|
88
|
+
type Works = IncludesWithoutIf<HundredZeroes, '1'>;
|
|
89
|
+
//=> 'not found'
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
@category Type Guard
|
|
93
|
+
@category Utilities
|
|
94
|
+
*/
|
|
95
|
+
export type If<Type extends boolean, IfBranch, ElseBranch> =
|
|
96
|
+
IsNever<Type> extends true
|
|
97
|
+
? ElseBranch
|
|
98
|
+
: Type extends true
|
|
99
|
+
? IfBranch
|
|
100
|
+
: ElseBranch;
|
|
101
|
+
|
|
102
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type {IsEqual} from './is-equal.d.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Returns a boolean for whether the given array includes the given item.
|
|
5
|
+
|
|
6
|
+
This can be useful if another type wants to make a decision based on whether the array includes that item.
|
|
7
|
+
|
|
8
|
+
@example
|
|
9
|
+
```
|
|
10
|
+
import type {Includes} from 'type-fest';
|
|
11
|
+
|
|
12
|
+
type hasRed<array extends any[]> = Includes<array, 'red'>;
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
@category Array
|
|
16
|
+
*/
|
|
17
|
+
export 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
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type {IntRange} from './int-range.d.ts';
|
|
2
|
+
import type {Sum} from './sum.d.ts';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
Generate a union of numbers.
|
|
6
|
+
|
|
7
|
+
The numbers are created from the given `Start` (inclusive) parameter to the given `End` (inclusive) parameter.
|
|
8
|
+
|
|
9
|
+
You skip over numbers using the `Step` parameter (defaults to `1`). For example, `IntClosedRange<0, 10, 2>` will create a union of `0 | 2 | 4 | 6 | 8 | 10`.
|
|
10
|
+
|
|
11
|
+
Note: `Start` or `End` must be non-negative and smaller than `999`.
|
|
12
|
+
|
|
13
|
+
Use-cases:
|
|
14
|
+
1. This can be used to define a set of valid input/output values. for example:
|
|
15
|
+
|
|
16
|
+
@example
|
|
17
|
+
```
|
|
18
|
+
import type {IntClosedRange} from 'type-fest';
|
|
19
|
+
|
|
20
|
+
type Age = IntClosedRange<0, 20>;
|
|
21
|
+
//=> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20
|
|
22
|
+
|
|
23
|
+
type FontSize = IntClosedRange<10, 20>;
|
|
24
|
+
//=> 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20
|
|
25
|
+
|
|
26
|
+
type EvenNumber = IntClosedRange<0, 10, 2>;
|
|
27
|
+
//=> 0 | 2 | 4 | 6 | 8 | 10
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
2. This can be used to define random numbers in a range. For example, `type RandomNumber = IntClosedRange<0, 100>;`
|
|
31
|
+
|
|
32
|
+
@example
|
|
33
|
+
```
|
|
34
|
+
import type {IntClosedRange} from 'type-fest';
|
|
35
|
+
|
|
36
|
+
type ZeroToNine = IntClosedRange<0, 9>;
|
|
37
|
+
//=> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
|
|
38
|
+
|
|
39
|
+
type Hundreds = IntClosedRange<100, 900, 100>;
|
|
40
|
+
//=> 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
@see {@link IntRange}
|
|
44
|
+
*/
|
|
45
|
+
export type IntClosedRange<Start extends number, End extends number, Skip extends number = 1> = IntRange<Start, Sum<End, 1>, Skip>;
|
|
46
|
+
|
|
47
|
+
export {};
|