@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,70 @@
|
|
|
1
|
+
import type {If} from './if.d.ts';
|
|
2
|
+
import type {IfNotAnyOrNever, IsArrayReadonly} from './internal/index.d.ts';
|
|
3
|
+
import type {UnknownArray} from './unknown-array.d.ts';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
Extract the type of an array or tuple minus the first element.
|
|
7
|
+
|
|
8
|
+
@example
|
|
9
|
+
```
|
|
10
|
+
import type {ArrayTail} from 'type-fest';
|
|
11
|
+
|
|
12
|
+
type A = ArrayTail<[1, 2, 3]>;
|
|
13
|
+
//=> [2, 3]
|
|
14
|
+
|
|
15
|
+
type B = ArrayTail<readonly [1, 2, 3]>;
|
|
16
|
+
//=> readonly [2, 3]
|
|
17
|
+
|
|
18
|
+
type C = ArrayTail<[1, 2, 3?, ...string[]]>;
|
|
19
|
+
//=> [2, 3?, ...string[]]
|
|
20
|
+
|
|
21
|
+
type D = ArrayTail<readonly [1]>;
|
|
22
|
+
//=> readonly []
|
|
23
|
+
|
|
24
|
+
type E = ArrayTail<[]>;
|
|
25
|
+
//=> []
|
|
26
|
+
|
|
27
|
+
type F = ArrayTail<string[]>;
|
|
28
|
+
//=> string[]
|
|
29
|
+
|
|
30
|
+
type G = ArrayTail<readonly [...string[], 1, 2]>;
|
|
31
|
+
//=> readonly [...string[], 1, 2]
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
@example
|
|
35
|
+
```
|
|
36
|
+
import type {ArrayTail} from 'type-fest';
|
|
37
|
+
|
|
38
|
+
type Curry<Func> = Func extends (...agruments_: infer Arguments) => infer Return
|
|
39
|
+
? Arguments extends readonly []
|
|
40
|
+
? Return
|
|
41
|
+
: (agrument: Arguments[0]) => Curry<(...agruments_: ArrayTail<Arguments>) => Return>
|
|
42
|
+
: never;
|
|
43
|
+
|
|
44
|
+
declare function curry<Func extends Function>(fn: Func): Curry<Func>;
|
|
45
|
+
|
|
46
|
+
declare function searchBooks(genre: string, minRating: number, available: boolean): string[];
|
|
47
|
+
|
|
48
|
+
const availableTopSciFi = curry(searchBooks)('sci-fi')(4.5)(true);
|
|
49
|
+
//=> string[]
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
@category Array
|
|
53
|
+
*/
|
|
54
|
+
export type ArrayTail<TArray extends UnknownArray> = IfNotAnyOrNever<TArray,
|
|
55
|
+
TArray extends UnknownArray // For distributing `TArray`
|
|
56
|
+
? _ArrayTail<TArray> extends infer Result
|
|
57
|
+
? If<IsArrayReadonly<TArray>, Readonly<Result>, Result>
|
|
58
|
+
: never // Should never happen
|
|
59
|
+
: never
|
|
60
|
+
>;
|
|
61
|
+
|
|
62
|
+
type _ArrayTail<TArray extends UnknownArray> = TArray extends readonly [unknown?, ...infer Tail]
|
|
63
|
+
? keyof TArray & `${number}` extends never
|
|
64
|
+
? TArray extends readonly []
|
|
65
|
+
? []
|
|
66
|
+
: TArray // Happens when `TArray` is a non-tuple array (e.g., `string[]`) or has a leading rest element (e.g., `[...string[], number]`)
|
|
67
|
+
: Tail
|
|
68
|
+
: [];
|
|
69
|
+
|
|
70
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Provides all values for a constant array or tuple.
|
|
3
|
+
|
|
4
|
+
Use-case: This type is useful when working with constant arrays or tuples and you want to enforce type-safety with their values.
|
|
5
|
+
|
|
6
|
+
@example
|
|
7
|
+
```
|
|
8
|
+
import type {ArrayValues, ArrayIndices} from 'type-fest';
|
|
9
|
+
|
|
10
|
+
const weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] as const;
|
|
11
|
+
|
|
12
|
+
type WeekdayName = ArrayValues<typeof weekdays>;
|
|
13
|
+
type Weekday = ArrayIndices<typeof weekdays>;
|
|
14
|
+
|
|
15
|
+
const getWeekdayName = (day: Weekday): WeekdayName => weekdays[day];
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
@see {@link ArrayIndices}
|
|
19
|
+
|
|
20
|
+
@category Array
|
|
21
|
+
*/
|
|
22
|
+
export type ArrayValues<T extends readonly unknown[]> = T[number];
|
|
23
|
+
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Create a type that represents either the value or an array of the value.
|
|
3
|
+
|
|
4
|
+
@see {@link Promisable}
|
|
5
|
+
|
|
6
|
+
@example
|
|
7
|
+
```
|
|
8
|
+
import type {Arrayable} from 'type-fest';
|
|
9
|
+
|
|
10
|
+
function bundle(input: string, output: Arrayable<string>) {
|
|
11
|
+
const outputList = Array.isArray(output) ? output : [output];
|
|
12
|
+
|
|
13
|
+
// …
|
|
14
|
+
|
|
15
|
+
for (const output of outputList) {
|
|
16
|
+
console.log(`write ${input} to: ${output}`);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
bundle('src/index.js', 'dist/index.js');
|
|
21
|
+
bundle('src/index.js', ['dist/index.cjs', 'dist/index.mjs']);
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
@category Array
|
|
25
|
+
*/
|
|
26
|
+
export type Arrayable<T> =
|
|
27
|
+
T
|
|
28
|
+
// TODO: Use `readonly T[]` when this issue is resolved: https://github.com/microsoft/TypeScript/issues/17002
|
|
29
|
+
| T[];
|
|
30
|
+
|
|
31
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
type AsyncFunction = (...arguments_: any[]) => PromiseLike<unknown>;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Unwrap the return type of a function that returns a `Promise`.
|
|
5
|
+
|
|
6
|
+
There has been [discussion](https://github.com/microsoft/TypeScript/pull/35998) about implementing this type in TypeScript.
|
|
7
|
+
|
|
8
|
+
@example
|
|
9
|
+
```ts
|
|
10
|
+
import type {AsyncReturnType} from 'type-fest';
|
|
11
|
+
|
|
12
|
+
declare function asyncFunction(): Promise<{foo: string}>;
|
|
13
|
+
|
|
14
|
+
// This type resolves to the unwrapped return type of `asyncFunction`.
|
|
15
|
+
type Value = AsyncReturnType<typeof asyncFunction>;
|
|
16
|
+
//=> {foo: string}
|
|
17
|
+
|
|
18
|
+
declare function doSomething(value: Value): void;
|
|
19
|
+
|
|
20
|
+
const value = await asyncFunction();
|
|
21
|
+
doSomething(value);
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
@category Async
|
|
25
|
+
*/
|
|
26
|
+
export type AsyncReturnType<Target extends AsyncFunction> = Awaited<ReturnType<Target>>;
|
|
27
|
+
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type {SetReturnType} from './set-return-type.d.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Create an async version of the given function type, by boxing the return type in `Promise` while keeping the same parameter types.
|
|
5
|
+
|
|
6
|
+
Use-case: You have two functions, one synchronous and one asynchronous that do the same thing. Instead of having to duplicate the type definition, you can use `Asyncify` to reuse the synchronous type.
|
|
7
|
+
|
|
8
|
+
@example
|
|
9
|
+
```
|
|
10
|
+
import type {Asyncify} from 'type-fest';
|
|
11
|
+
|
|
12
|
+
// Synchronous function
|
|
13
|
+
type Config = {featureFlags: Record<string, boolean>};
|
|
14
|
+
|
|
15
|
+
declare function loadConfigSync(path: string): Config;
|
|
16
|
+
|
|
17
|
+
type LoadConfigAsync = Asyncify<typeof loadConfigSync>;
|
|
18
|
+
//=> (path: string) => Promise<Config>
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
@category Async
|
|
22
|
+
*/
|
|
23
|
+
export type Asyncify<Function_ extends (...arguments_: any[]) => any> = SetReturnType<Function_, Promise<Awaited<ReturnType<Function_>>>>;
|
|
24
|
+
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Matches a [`class`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
|
|
3
|
+
|
|
4
|
+
@category Class
|
|
5
|
+
*/
|
|
6
|
+
export type Class<T, Arguments extends unknown[] = any[]> = {
|
|
7
|
+
prototype: Pick<T, keyof T>;
|
|
8
|
+
new(...arguments_: Arguments): T;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
|
|
13
|
+
|
|
14
|
+
@category Class
|
|
15
|
+
*/
|
|
16
|
+
export type Constructor<T, Arguments extends unknown[] = any[]> = new(...arguments_: Arguments) => T;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
Matches an [`abstract class`](https://www.typescriptlang.org/docs/handbook/2/classes.html#abstract-classes-and-members).
|
|
20
|
+
|
|
21
|
+
@category Class
|
|
22
|
+
|
|
23
|
+
@privateRemarks
|
|
24
|
+
We cannot use a `type` here because TypeScript throws: 'abstract' modifier cannot appear on a type member. (1070)
|
|
25
|
+
*/
|
|
26
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
|
|
27
|
+
export interface AbstractClass<T, Arguments extends unknown[] = any[]> extends AbstractConstructor<T, Arguments> {
|
|
28
|
+
prototype: Pick<T, keyof T>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
Matches an [`abstract class`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-2.html#abstract-construct-signatures) constructor.
|
|
33
|
+
|
|
34
|
+
@category Class
|
|
35
|
+
*/
|
|
36
|
+
export type AbstractConstructor<T, Arguments extends unknown[] = any[]> = abstract new(...arguments_: Arguments) => T;
|
|
37
|
+
|
|
38
|
+
export {};
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type {ApplyDefaultOptions} from './internal/index.d.ts';
|
|
2
|
+
import type {Words, WordsOptions} from './words.d.ts';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
CamelCase options.
|
|
6
|
+
|
|
7
|
+
@see {@link CamelCase}
|
|
8
|
+
*/
|
|
9
|
+
export type CamelCaseOptions = WordsOptions & {
|
|
10
|
+
/**
|
|
11
|
+
Whether to preserved consecutive uppercase letter.
|
|
12
|
+
|
|
13
|
+
@default false
|
|
14
|
+
*/
|
|
15
|
+
preserveConsecutiveUppercase?: boolean;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type _DefaultCamelCaseOptions = {
|
|
19
|
+
splitOnNumbers: true;
|
|
20
|
+
preserveConsecutiveUppercase: false;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
Convert an array of words to camel-case.
|
|
25
|
+
*/
|
|
26
|
+
type CamelCaseFromArray<
|
|
27
|
+
Words extends string[],
|
|
28
|
+
Options extends Required<CamelCaseOptions>,
|
|
29
|
+
OutputString extends string = '',
|
|
30
|
+
> = Words extends [
|
|
31
|
+
infer FirstWord extends string,
|
|
32
|
+
...infer RemainingWords extends string[],
|
|
33
|
+
]
|
|
34
|
+
? Options['preserveConsecutiveUppercase'] extends true
|
|
35
|
+
? `${Capitalize<FirstWord>}${CamelCaseFromArray<RemainingWords, Options>}`
|
|
36
|
+
: `${Capitalize<Lowercase<FirstWord>>}${CamelCaseFromArray<RemainingWords, Options>}`
|
|
37
|
+
: OutputString;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
Convert a string literal to camel-case.
|
|
41
|
+
|
|
42
|
+
This can be useful when, for example, converting some kebab-cased command-line flags or a snake-cased database result.
|
|
43
|
+
|
|
44
|
+
By default, consecutive uppercase letter are preserved. See {@link CamelCaseOptions.preserveConsecutiveUppercase preserveConsecutiveUppercase} option to change this behaviour.
|
|
45
|
+
|
|
46
|
+
@example
|
|
47
|
+
```
|
|
48
|
+
import type {CamelCase} from 'type-fest';
|
|
49
|
+
|
|
50
|
+
// Simple
|
|
51
|
+
|
|
52
|
+
const someVariable: CamelCase<'foo-bar'> = 'fooBar';
|
|
53
|
+
const preserveConsecutiveUppercase: CamelCase<'foo-BAR-baz', {preserveConsecutiveUppercase: true}> = 'fooBARBaz';
|
|
54
|
+
|
|
55
|
+
// Advanced
|
|
56
|
+
|
|
57
|
+
type CamelCasedProperties<T> = {
|
|
58
|
+
[K in keyof T as CamelCase<K>]: T[K]
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
type RawOptions = {
|
|
62
|
+
'dry-run': boolean;
|
|
63
|
+
'full_family_name': string;
|
|
64
|
+
foo: number;
|
|
65
|
+
BAR: string;
|
|
66
|
+
QUZ_QUX: number;
|
|
67
|
+
'OTHER-FIELD': boolean;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const dbResult: CamelCasedProperties<RawOptions> = {
|
|
71
|
+
dryRun: true,
|
|
72
|
+
fullFamilyName: 'bar.js',
|
|
73
|
+
foo: 123,
|
|
74
|
+
bar: 'foo',
|
|
75
|
+
quzQux: 6,
|
|
76
|
+
otherField: false,
|
|
77
|
+
};
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
@category Change case
|
|
81
|
+
@category Template literal
|
|
82
|
+
*/
|
|
83
|
+
export type CamelCase<Type, Options extends CamelCaseOptions = {}> = Type extends string
|
|
84
|
+
? string extends Type
|
|
85
|
+
? Type
|
|
86
|
+
: Uncapitalize<CamelCaseFromArray<
|
|
87
|
+
Words<Type extends Uppercase<Type> ? Lowercase<Type> : Type, Options>,
|
|
88
|
+
ApplyDefaultOptions<CamelCaseOptions, _DefaultCamelCaseOptions, Options>
|
|
89
|
+
>>
|
|
90
|
+
: Type;
|
|
91
|
+
|
|
92
|
+
export {};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type {CamelCase, CamelCaseOptions, _DefaultCamelCaseOptions} from './camel-case.d.ts';
|
|
2
|
+
import type {ApplyDefaultOptions, NonRecursiveType} from './internal/index.d.ts';
|
|
3
|
+
import type {UnknownArray} from './unknown-array.d.ts';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
Convert object properties to camel case recursively.
|
|
7
|
+
|
|
8
|
+
This can be useful when, for example, converting some API types from a different style.
|
|
9
|
+
|
|
10
|
+
@see {@link CamelCasedProperties}
|
|
11
|
+
@see {@link CamelCase}
|
|
12
|
+
|
|
13
|
+
@example
|
|
14
|
+
```
|
|
15
|
+
import type {CamelCasedPropertiesDeep} from 'type-fest';
|
|
16
|
+
|
|
17
|
+
type User = {
|
|
18
|
+
UserId: number;
|
|
19
|
+
UserName: string;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
type UserWithFriends = {
|
|
23
|
+
UserInfo: User;
|
|
24
|
+
UserFriends: User[];
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const result: CamelCasedPropertiesDeep<UserWithFriends> = {
|
|
28
|
+
userInfo: {
|
|
29
|
+
userId: 1,
|
|
30
|
+
userName: 'Tom',
|
|
31
|
+
},
|
|
32
|
+
userFriends: [
|
|
33
|
+
{
|
|
34
|
+
userId: 2,
|
|
35
|
+
userName: 'Jerry',
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
userId: 3,
|
|
39
|
+
userName: 'Spike',
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const preserveConsecutiveUppercase: CamelCasedPropertiesDeep<{fooBAR: {fooBARBiz: [{fooBARBaz: string}]}}, {preserveConsecutiveUppercase: true}> = {
|
|
45
|
+
fooBAR: {
|
|
46
|
+
fooBARBiz: [{
|
|
47
|
+
fooBARBaz: 'string',
|
|
48
|
+
}],
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
@category Change case
|
|
54
|
+
@category Template literal
|
|
55
|
+
@category Object
|
|
56
|
+
*/
|
|
57
|
+
export type CamelCasedPropertiesDeep<
|
|
58
|
+
Value,
|
|
59
|
+
Options extends CamelCaseOptions = {},
|
|
60
|
+
> = _CamelCasedPropertiesDeep<Value, ApplyDefaultOptions<CamelCaseOptions, _DefaultCamelCaseOptions, Options>>;
|
|
61
|
+
|
|
62
|
+
type _CamelCasedPropertiesDeep<
|
|
63
|
+
Value,
|
|
64
|
+
Options extends Required<CamelCaseOptions>,
|
|
65
|
+
> = Value extends NonRecursiveType
|
|
66
|
+
? Value
|
|
67
|
+
: Value extends UnknownArray
|
|
68
|
+
? CamelCasedPropertiesArrayDeep<Value, Options>
|
|
69
|
+
: Value extends Set<infer U>
|
|
70
|
+
? Set<_CamelCasedPropertiesDeep<U, Options>>
|
|
71
|
+
: Value extends object
|
|
72
|
+
? {
|
|
73
|
+
[K in keyof Value as CamelCase<K, Options>]: _CamelCasedPropertiesDeep<Value[K], Options>;
|
|
74
|
+
}
|
|
75
|
+
: Value;
|
|
76
|
+
|
|
77
|
+
// This is a copy of DelimiterCasedPropertiesArrayDeep (see: delimiter-cased-properties-deep.d.ts).
|
|
78
|
+
// These types should be kept in sync.
|
|
79
|
+
type CamelCasedPropertiesArrayDeep<
|
|
80
|
+
Value extends UnknownArray,
|
|
81
|
+
Options extends Required<CamelCaseOptions>,
|
|
82
|
+
> = Value extends []
|
|
83
|
+
? []
|
|
84
|
+
// Trailing spread array
|
|
85
|
+
: Value extends [infer U, ...infer V]
|
|
86
|
+
? [_CamelCasedPropertiesDeep<U, Options>, ..._CamelCasedPropertiesDeep<V, Options>]
|
|
87
|
+
: Value extends readonly [infer U, ...infer V]
|
|
88
|
+
? readonly [_CamelCasedPropertiesDeep<U, Options>, ..._CamelCasedPropertiesDeep<V, Options>]
|
|
89
|
+
: // Leading spread array
|
|
90
|
+
Value extends readonly [...infer U, infer V]
|
|
91
|
+
? [..._CamelCasedPropertiesDeep<U, Options>, _CamelCasedPropertiesDeep<V, Options>]
|
|
92
|
+
: // Array
|
|
93
|
+
Value extends Array<infer U>
|
|
94
|
+
? Array<_CamelCasedPropertiesDeep<U, Options>>
|
|
95
|
+
: Value extends ReadonlyArray<infer U>
|
|
96
|
+
? ReadonlyArray<_CamelCasedPropertiesDeep<U, Options>>
|
|
97
|
+
: never;
|
|
98
|
+
|
|
99
|
+
export {};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type {CamelCase, CamelCaseOptions, _DefaultCamelCaseOptions} from './camel-case.d.ts';
|
|
2
|
+
import type {ApplyDefaultOptions} from './internal/index.d.ts';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
Convert object properties to camel case but not recursively.
|
|
6
|
+
|
|
7
|
+
This can be useful when, for example, converting some API types from a different style.
|
|
8
|
+
|
|
9
|
+
@see {@link CamelCasedPropertiesDeep}
|
|
10
|
+
@see {@link CamelCase}
|
|
11
|
+
|
|
12
|
+
@example
|
|
13
|
+
```
|
|
14
|
+
import type {CamelCasedProperties} from 'type-fest';
|
|
15
|
+
|
|
16
|
+
type User = {
|
|
17
|
+
UserId: number;
|
|
18
|
+
UserName: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const result: CamelCasedProperties<User> = {
|
|
22
|
+
userId: 1,
|
|
23
|
+
userName: 'Tom',
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const preserveConsecutiveUppercase: CamelCasedProperties<{fooBAR: string}, {preserveConsecutiveUppercase: true}> = {
|
|
27
|
+
fooBAR: 'string',
|
|
28
|
+
};
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
@category Change case
|
|
32
|
+
@category Template literal
|
|
33
|
+
@category Object
|
|
34
|
+
*/
|
|
35
|
+
export type CamelCasedProperties<Value, Options extends CamelCaseOptions = {}> = Value extends Function
|
|
36
|
+
? Value
|
|
37
|
+
: Value extends Array<infer U>
|
|
38
|
+
? Value
|
|
39
|
+
: {
|
|
40
|
+
[K in keyof Value as
|
|
41
|
+
CamelCase<K, ApplyDefaultOptions<CamelCaseOptions, _DefaultCamelCaseOptions, Options>>
|
|
42
|
+
]: Value[K];
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export {};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Matches any uppercase letter in the basic Latin alphabet (A-Z).
|
|
3
|
+
|
|
4
|
+
@example
|
|
5
|
+
```
|
|
6
|
+
import type {UppercaseLetter} from 'type-fest';
|
|
7
|
+
|
|
8
|
+
const a: UppercaseLetter = 'A'; // Valid
|
|
9
|
+
// @ts-expect-error
|
|
10
|
+
const b: UppercaseLetter = 'a'; // Invalid
|
|
11
|
+
// @ts-expect-error
|
|
12
|
+
const c: UppercaseLetter = 'AB'; // Invalid
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
@category Type
|
|
16
|
+
*/
|
|
17
|
+
export type UppercaseLetter = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
Matches any lowercase letter in the basic Latin alphabet (a-z).
|
|
21
|
+
|
|
22
|
+
@example
|
|
23
|
+
```
|
|
24
|
+
import type {LowercaseLetter} from 'type-fest';
|
|
25
|
+
|
|
26
|
+
const a: LowercaseLetter = 'a'; // Valid
|
|
27
|
+
// @ts-expect-error
|
|
28
|
+
const b: LowercaseLetter = 'A'; // Invalid
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
@category Type
|
|
32
|
+
*/
|
|
33
|
+
export type LowercaseLetter = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i' | 'j' | 'k' | 'l' | 'm' | 'n' | 'o' | 'p' | 'q' | 'r' | 's' | 't' | 'u' | 'v' | 'w' | 'x' | 'y' | 'z';
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
Matches any digit as a string ('0'-'9').
|
|
37
|
+
|
|
38
|
+
@example
|
|
39
|
+
```
|
|
40
|
+
import type {DigitCharacter} from 'type-fest';
|
|
41
|
+
|
|
42
|
+
const a: DigitCharacter = '0'; // Valid
|
|
43
|
+
// @ts-expect-error
|
|
44
|
+
const b: DigitCharacter = 0; // Invalid
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
@category Type
|
|
48
|
+
*/
|
|
49
|
+
export type DigitCharacter = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
Matches any lowercase letter (a-z), uppercase letter (A-Z), or digit ('0'-'9') in the basic Latin alphabet.
|
|
53
|
+
|
|
54
|
+
@example
|
|
55
|
+
```
|
|
56
|
+
import type {Alphanumeric} from 'type-fest';
|
|
57
|
+
|
|
58
|
+
const a: Alphanumeric = 'A'; // Valid
|
|
59
|
+
// @ts-expect-error
|
|
60
|
+
const b: Alphanumeric = '#'; // Invalid
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
@category Type
|
|
64
|
+
*/
|
|
65
|
+
export type Alphanumeric = LowercaseLetter | UppercaseLetter | DigitCharacter;
|
|
66
|
+
|
|
67
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type {Except} from './except.d.ts';
|
|
2
|
+
import type {ConditionalKeys} from './conditional-keys.d.ts';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
Exclude keys from a shape that matches the given `Condition`.
|
|
6
|
+
|
|
7
|
+
This is useful when you want to create a new type with a specific set of keys from a shape. For example, you might want to exclude all the primitive properties from a class and form a new shape containing everything but the primitive properties.
|
|
8
|
+
|
|
9
|
+
@example
|
|
10
|
+
```
|
|
11
|
+
import type {Primitive, ConditionalExcept} from 'type-fest';
|
|
12
|
+
|
|
13
|
+
class Awesome {
|
|
14
|
+
constructor(public name: string, public successes: number, public failures: bigint) {}
|
|
15
|
+
|
|
16
|
+
run() {
|
|
17
|
+
// do something
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
type ExceptPrimitivesFromAwesome = ConditionalExcept<Awesome, Primitive>;
|
|
22
|
+
//=> {run: () => void}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
@example
|
|
26
|
+
```
|
|
27
|
+
import type {ConditionalExcept} from 'type-fest';
|
|
28
|
+
|
|
29
|
+
type Example = {
|
|
30
|
+
a: string;
|
|
31
|
+
b: string | number;
|
|
32
|
+
c: () => void;
|
|
33
|
+
d: {};
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
type NonStringKeysOnly = ConditionalExcept<Example, string>;
|
|
37
|
+
//=> {b: string | number; c: () => void; d: {}}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
@category Object
|
|
41
|
+
*/
|
|
42
|
+
export type ConditionalExcept<Base, Condition> = Except<
|
|
43
|
+
Base,
|
|
44
|
+
ConditionalKeys<Base, Condition>
|
|
45
|
+
>;
|
|
46
|
+
|
|
47
|
+
export {};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type {ExtendsStrict} from './extends-strict.d.ts';
|
|
2
|
+
import type {IfNotAnyOrNever} from './internal/type.d.ts';
|
|
3
|
+
import type {TupleToObject} from './tuple-to-object.d.ts';
|
|
4
|
+
import type {UnknownArray} from './unknown-array.d.ts';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
Extract the keys from a type where the value type of the key extends the given `Condition`.
|
|
8
|
+
|
|
9
|
+
Internally this is used for the `ConditionalPick` and `ConditionalExcept` types.
|
|
10
|
+
|
|
11
|
+
@example
|
|
12
|
+
```
|
|
13
|
+
import type {ConditionalKeys} from 'type-fest';
|
|
14
|
+
|
|
15
|
+
type Example = {
|
|
16
|
+
a: string;
|
|
17
|
+
b: string | number;
|
|
18
|
+
c?: string;
|
|
19
|
+
d: {};
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
type StringKeysOnly = ConditionalKeys<Example, string>;
|
|
23
|
+
//=> 'a'
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Note: To extract optional keys, make sure your `Condition` is a union of `undefined` (for example, `string | undefined`) as demonstrated below.
|
|
27
|
+
|
|
28
|
+
@example
|
|
29
|
+
```
|
|
30
|
+
import type {ConditionalKeys} from 'type-fest';
|
|
31
|
+
|
|
32
|
+
type StringKeysAndUndefined = ConditionalKeys<{a?: string}, string | undefined>;
|
|
33
|
+
//=> 'a'
|
|
34
|
+
|
|
35
|
+
type NoMatchingKeys = ConditionalKeys<{a?: string}, string>;
|
|
36
|
+
//=> never
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
You can also extract array indices whose value match the specified condition, as shown below:
|
|
40
|
+
```
|
|
41
|
+
import type {ConditionalKeys} from 'type-fest';
|
|
42
|
+
|
|
43
|
+
type StringValueIndices = ConditionalKeys<[string, number, string], string>;
|
|
44
|
+
//=> '0' | '2'
|
|
45
|
+
|
|
46
|
+
type NumberValueIndices = ConditionalKeys<[string, number?, string?], number | undefined>;
|
|
47
|
+
//=> '1'
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
@category Object
|
|
51
|
+
*/
|
|
52
|
+
export type ConditionalKeys<Base, Condition> = (Base extends UnknownArray ? TupleToObject<Base> : Base) extends infer _Base // Remove non-numeric keys from arrays
|
|
53
|
+
? IfNotAnyOrNever<_Base, _ConditionalKeys<_Base, Condition>, keyof _Base>
|
|
54
|
+
: never;
|
|
55
|
+
|
|
56
|
+
type _ConditionalKeys<Base, Condition> = keyof {
|
|
57
|
+
[
|
|
58
|
+
Key in (keyof Base & {}) as // `& {}` prevents homomorphism
|
|
59
|
+
ExtendsStrict<Base[Key], Condition> extends true ? Key : never
|
|
60
|
+
]: never
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export {};
|