type-fest 2.12.2 → 2.13.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/index.d.ts +4 -0
- package/package.json +4 -4
- package/readme.md +73 -2
- package/source/async-return-type.d.ts +2 -2
- package/source/asyncify.d.ts +3 -3
- package/source/camel-case.d.ts +3 -3
- package/source/camel-cased-properties-deep.d.ts +3 -1
- package/source/camel-cased-properties.d.ts +3 -1
- package/source/conditional-except.d.ts +4 -4
- package/source/conditional-keys.d.ts +3 -1
- package/source/conditional-pick.d.ts +3 -3
- package/source/delimiter-case.d.ts +2 -2
- package/source/delimiter-cased-properties-deep.d.ts +3 -1
- package/source/delimiter-cased-properties.d.ts +3 -1
- package/source/entries.d.ts +2 -2
- package/source/entry.d.ts +1 -1
- package/source/exact.d.ts +51 -0
- package/source/except.d.ts +2 -2
- package/source/fixed-length-array.d.ts +4 -1
- package/source/get.d.ts +4 -4
- package/source/includes.d.ts +2 -2
- package/source/internal.d.ts +10 -1
- package/source/invariant-of.d.ts +5 -1
- package/source/iterable-element.d.ts +6 -0
- package/source/join.d.ts +1 -1
- package/source/jsonify.d.ts +5 -1
- package/source/kebab-case.d.ts +2 -2
- package/source/kebab-cased-properties-deep.d.ts +3 -1
- package/source/kebab-cased-properties.d.ts +3 -1
- package/source/last-array-element.d.ts +1 -1
- package/source/literal-to-primitive.d.ts +1 -1
- package/source/literal-union.d.ts +2 -2
- package/source/merge-exclusive.d.ts +1 -1
- package/source/merge.d.ts +3 -3
- package/source/multidimensional-array.d.ts +2 -2
- package/source/multidimensional-readonly-array.d.ts +2 -2
- package/source/mutable.d.ts +3 -38
- package/source/numeric.d.ts +5 -5
- package/source/opaque.d.ts +1 -1
- package/source/package-json.d.ts +8 -8
- package/source/partial-deep.d.ts +2 -2
- package/source/pascal-case.d.ts +2 -2
- package/source/pascal-cased-properties-deep.d.ts +3 -1
- package/source/pascal-cased-properties.d.ts +3 -1
- package/source/promisable.d.ts +1 -1
- package/source/promise-value.d.ts +4 -4
- package/source/readonly-deep.d.ts +2 -2
- package/source/readonly-tuple.d.ts +41 -0
- package/source/remove-index-signature.d.ts +8 -2
- package/source/replace.d.ts +67 -0
- package/source/require-all-or-none.d.ts +1 -1
- package/source/require-at-least-one.d.ts +2 -2
- package/source/require-exactly-one.d.ts +1 -1
- package/source/schema.d.ts +1 -1
- package/source/screaming-snake-case.d.ts +4 -4
- package/source/set-optional.d.ts +3 -3
- package/source/set-required.d.ts +3 -3
- package/source/set-return-type.d.ts +1 -1
- package/source/simplify.d.ts +2 -2
- package/source/snake-case.d.ts +2 -2
- package/source/snake-cased-properties-deep.d.ts +3 -1
- package/source/snake-cased-properties.d.ts +3 -1
- package/source/split.d.ts +1 -1
- package/source/string-key-of.d.ts +1 -1
- package/source/stringified.d.ts +1 -1
- package/source/trim.d.ts +1 -1
- package/source/tsconfig-json.d.ts +1 -1
- package/source/union-to-intersection.d.ts +2 -2
- package/source/value-of.d.ts +1 -1
- package/source/writable.d.ts +40 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {DelimiterCasedProperties} from './delimiter-cased-properties';
|
|
1
|
+
import type {DelimiterCasedProperties} from './delimiter-cased-properties';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
Convert object properties to kebab case but not recursively.
|
|
@@ -10,6 +10,8 @@ This can be useful when, for example, converting some API types from a different
|
|
|
10
10
|
|
|
11
11
|
@example
|
|
12
12
|
```
|
|
13
|
+
import type {KebabCasedProperties} from 'type-fest';
|
|
14
|
+
|
|
13
15
|
interface User {
|
|
14
16
|
userId: number;
|
|
15
17
|
userName: string;
|
|
@@ -5,7 +5,7 @@ Use-case: Defining the return type of functions that extract the last element of
|
|
|
5
5
|
|
|
6
6
|
@example
|
|
7
7
|
```
|
|
8
|
-
import {LastArrayElement} from 'type-fest';
|
|
8
|
+
import type {LastArrayElement} from 'type-fest';
|
|
9
9
|
|
|
10
10
|
declare function lastOf<V extends readonly any[]>(array: V): LastArrayElement<V>;
|
|
11
11
|
|
|
@@ -5,7 +5,7 @@ Use-case: Working with generic types that may be literal types.
|
|
|
5
5
|
|
|
6
6
|
@example
|
|
7
7
|
```
|
|
8
|
-
import {LiteralToPrimitive} from 'type-fest';
|
|
8
|
+
import type {LiteralToPrimitive} from 'type-fest';
|
|
9
9
|
|
|
10
10
|
// No overloads needed to get the correct return type
|
|
11
11
|
function plus<T extends number | bigint | string>(x: T, y: T): LiteralToPrimitive<T> {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {Primitive} from './primitive';
|
|
1
|
+
import type {Primitive} from './primitive';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
Allows creating a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union.
|
|
@@ -9,7 +9,7 @@ This type is a workaround for [Microsoft/TypeScript#29729](https://github.com/Mi
|
|
|
9
9
|
|
|
10
10
|
@example
|
|
11
11
|
```
|
|
12
|
-
import {LiteralUnion} from 'type-fest';
|
|
12
|
+
import type {LiteralUnion} from 'type-fest';
|
|
13
13
|
|
|
14
14
|
// Before
|
|
15
15
|
|
package/source/merge.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {Except} from './except';
|
|
2
|
-
import {Simplify} from './simplify';
|
|
1
|
+
import type {Except} from './except';
|
|
2
|
+
import type {Simplify} from './simplify';
|
|
3
3
|
|
|
4
4
|
type Merge_<FirstType, SecondType> = Except<FirstType, Extract<keyof FirstType, keyof SecondType>> & SecondType;
|
|
5
5
|
|
|
@@ -8,7 +8,7 @@ Merge two types into a new type. Keys of the second type overrides keys of the f
|
|
|
8
8
|
|
|
9
9
|
@example
|
|
10
10
|
```
|
|
11
|
-
import {Merge} from 'type-fest';
|
|
11
|
+
import type {Merge} from 'type-fest';
|
|
12
12
|
|
|
13
13
|
type Foo = {
|
|
14
14
|
a: number;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {IsEqual, Subtract} from './internal';
|
|
1
|
+
import type {IsEqual, Subtract} from './internal';
|
|
2
2
|
|
|
3
3
|
type Recursive<T> = Array<Recursive<T>>;
|
|
4
4
|
|
|
@@ -13,7 +13,7 @@ Use-cases:
|
|
|
13
13
|
|
|
14
14
|
@example
|
|
15
15
|
```
|
|
16
|
-
import {MultidimensionalArray} from 'type-fest';
|
|
16
|
+
import type {MultidimensionalArray} from 'type-fest';
|
|
17
17
|
|
|
18
18
|
function emptyMatrix<T extends number>(dimensions: T): MultidimensionalArray<unknown, T> {
|
|
19
19
|
const matrix: unknown[] = [];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {IsEqual, Subtract} from './internal';
|
|
1
|
+
import type {IsEqual, Subtract} from './internal';
|
|
2
2
|
|
|
3
3
|
type Recursive<T> = ReadonlyArray<Recursive<T>>;
|
|
4
4
|
|
|
@@ -13,7 +13,7 @@ Use-cases:
|
|
|
13
13
|
|
|
14
14
|
@example
|
|
15
15
|
```
|
|
16
|
-
import {MultidimensionalReadonlyArray} from 'type-fest';
|
|
16
|
+
import type {MultidimensionalReadonlyArray} from 'type-fest';
|
|
17
17
|
|
|
18
18
|
function emptyMatrix<T extends number>(dimensions: T): MultidimensionalReadonlyArray<unknown, T> {
|
|
19
19
|
const matrix: unknown[] = [];
|
package/source/mutable.d.ts
CHANGED
|
@@ -1,40 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {Simplify} from './simplify';
|
|
1
|
+
import type {Writable} from './writable';
|
|
3
2
|
|
|
4
|
-
/**
|
|
5
|
-
Create a type that strips `readonly` from all or some of an object's keys. Inverse of `Readonly<T>`.
|
|
6
|
-
|
|
7
|
-
This can be used to [store and mutate options within a class](https://github.com/sindresorhus/pageres/blob/4a5d05fca19a5fbd2f53842cbf3eb7b1b63bddd2/source/index.ts#L72), [edit `readonly` objects within tests](https://stackoverflow.com/questions/50703834), [construct a `readonly` object within a function](https://github.com/Microsoft/TypeScript/issues/24509), or to define a single model where the only thing that changes is whether or not some of the keys are mutable.
|
|
8
|
-
|
|
9
|
-
@example
|
|
10
|
-
```
|
|
11
|
-
import {Mutable} from 'type-fest';
|
|
12
|
-
|
|
13
|
-
type Foo = {
|
|
14
|
-
readonly a: number;
|
|
15
|
-
readonly b: readonly string[]; // To show that only the mutability status of the properties, not their values, are affected.
|
|
16
|
-
readonly c: boolean;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const mutableFoo: Mutable<Foo> = {a: 1, b: ['2'], c: true};
|
|
20
|
-
mutableFoo.a = 3;
|
|
21
|
-
mutableFoo.b[0] = 'new value'; // Will still fail as the value of property "b" is still a readonly type.
|
|
22
|
-
mutableFoo.b = ['something']; // Will work as the "b" property itself is no longer readonly.
|
|
23
|
-
|
|
24
|
-
type SomeMutable = Mutable<Foo, 'b' | 'c'>;
|
|
25
|
-
// type SomeMutable = {
|
|
26
|
-
// readonly a: number;
|
|
27
|
-
// b: readonly string[]; // It's now mutable. The type of the property remains unaffected.
|
|
28
|
-
// c: boolean; // It's now mutable.
|
|
29
|
-
// }
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
@category Object
|
|
33
|
-
*/
|
|
3
|
+
/** @deprecated @see Writable */
|
|
34
4
|
export type Mutable<BaseType, Keys extends keyof BaseType = keyof BaseType> =
|
|
35
|
-
|
|
36
|
-
// Pick just the keys that are not mutable from the base type.
|
|
37
|
-
Except<BaseType, Keys> &
|
|
38
|
-
// Pick the keys that should be mutable from the base type and make them mutable by removing the `readonly` modifier from the key.
|
|
39
|
-
{-readonly [KeyType in keyof Pick<BaseType, Keys>]: Pick<BaseType, Keys>[KeyType]}
|
|
40
|
-
>;
|
|
5
|
+
Writable<BaseType, Keys>;
|
package/source/numeric.d.ts
CHANGED
|
@@ -36,7 +36,7 @@ Use-case: Validating and documenting parameters.
|
|
|
36
36
|
|
|
37
37
|
@example
|
|
38
38
|
```
|
|
39
|
-
import {Finite} from 'type-fest';
|
|
39
|
+
import type {Finite} from 'type-fest';
|
|
40
40
|
|
|
41
41
|
declare function setScore<T extends number>(length: Finite<T>): void;
|
|
42
42
|
```
|
|
@@ -53,7 +53,7 @@ Use-case: Validating and documenting parameters.
|
|
|
53
53
|
|
|
54
54
|
@example
|
|
55
55
|
```
|
|
56
|
-
import {Integer} from 'type-fest';
|
|
56
|
+
import type {Integer} from 'type-fest';
|
|
57
57
|
|
|
58
58
|
declare function setYear<T extends number>(length: Integer<T>): void;
|
|
59
59
|
```
|
|
@@ -75,7 +75,7 @@ Use-case: Validating and documenting parameters.
|
|
|
75
75
|
|
|
76
76
|
@example
|
|
77
77
|
```
|
|
78
|
-
import {Float} from 'type-fest';
|
|
78
|
+
import type {Float} from 'type-fest';
|
|
79
79
|
|
|
80
80
|
declare function setPercentage<T extends number>(length: Float<T>): void;
|
|
81
81
|
```
|
|
@@ -136,7 +136,7 @@ Use-case: Validating and documenting parameters.
|
|
|
136
136
|
|
|
137
137
|
@example
|
|
138
138
|
```
|
|
139
|
-
import {NonNegative} from 'type-fest';
|
|
139
|
+
import type {NonNegative} from 'type-fest';
|
|
140
140
|
|
|
141
141
|
declare function setLength<T extends number>(length: NonNegative<T>): void;
|
|
142
142
|
```
|
|
@@ -158,7 +158,7 @@ Use-case: Validating and documenting parameters.
|
|
|
158
158
|
|
|
159
159
|
@example
|
|
160
160
|
```
|
|
161
|
-
import {NonNegativeInteger} from 'type-fest';
|
|
161
|
+
import type {NonNegativeInteger} from 'type-fest';
|
|
162
162
|
|
|
163
163
|
declare function setLength<T extends number>(length: NonNegativeInteger<T>): void;
|
|
164
164
|
```
|
package/source/opaque.d.ts
CHANGED
|
@@ -18,7 +18,7 @@ There have been several discussions about adding this feature to TypeScript via
|
|
|
18
18
|
|
|
19
19
|
@example
|
|
20
20
|
```
|
|
21
|
-
import {Opaque} from 'type-fest';
|
|
21
|
+
import type {Opaque} from 'type-fest';
|
|
22
22
|
|
|
23
23
|
type AccountNumber = Opaque<number, 'AccountNumber'>;
|
|
24
24
|
type AccountBalance = Opaque<number, 'AccountBalance'>;
|
package/source/package-json.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {LiteralUnion} from './literal-union';
|
|
1
|
+
import type {LiteralUnion} from './literal-union';
|
|
2
2
|
|
|
3
3
|
declare namespace PackageJson {
|
|
4
4
|
/**
|
|
@@ -200,12 +200,12 @@ declare namespace PackageJson {
|
|
|
200
200
|
Run with the `npm restart` command, after `restart`. Note: `npm restart` will run the `stop` and `start` scripts if no `restart` script is provided.
|
|
201
201
|
*/
|
|
202
202
|
postrestart?: string;
|
|
203
|
-
} & Record<string, string
|
|
203
|
+
} & Partial<Record<string, string>>;
|
|
204
204
|
|
|
205
205
|
/**
|
|
206
206
|
Dependencies of the package. The version range is a string which has one or more space-separated descriptors. Dependencies can also be identified with a tarball or Git URL.
|
|
207
207
|
*/
|
|
208
|
-
export type Dependency = Record<string, string
|
|
208
|
+
export type Dependency = Partial<Record<string, string>>;
|
|
209
209
|
|
|
210
210
|
/**
|
|
211
211
|
Conditions which provide a way to resolve a package entry point based on the environment.
|
|
@@ -262,7 +262,7 @@ declare namespace PackageJson {
|
|
|
262
262
|
*/
|
|
263
263
|
browser?:
|
|
264
264
|
| string
|
|
265
|
-
| Record<string, string | false
|
|
265
|
+
| Partial<Record<string, string | false>>;
|
|
266
266
|
|
|
267
267
|
/**
|
|
268
268
|
Denote which files in your project are "pure" and therefore safe for Webpack to prune if unused.
|
|
@@ -281,7 +281,7 @@ declare namespace PackageJson {
|
|
|
281
281
|
/**
|
|
282
282
|
Version selection map of TypeScript.
|
|
283
283
|
*/
|
|
284
|
-
typesVersions?: Record<string, Record<string, string[]
|
|
284
|
+
typesVersions?: Partial<Record<string, Partial<Record<string, string[]>>>>;
|
|
285
285
|
|
|
286
286
|
/**
|
|
287
287
|
Location of the bundled TypeScript declaration file. Alias of `types`.
|
|
@@ -442,7 +442,7 @@ declare namespace PackageJson {
|
|
|
442
442
|
*/
|
|
443
443
|
bin?:
|
|
444
444
|
| string
|
|
445
|
-
| Record<string, string
|
|
445
|
+
| Partial<Record<string, string>>;
|
|
446
446
|
|
|
447
447
|
/**
|
|
448
448
|
Filenames to put in place for the `man` program to find.
|
|
@@ -504,7 +504,7 @@ declare namespace PackageJson {
|
|
|
504
504
|
/**
|
|
505
505
|
Indicate peer dependencies that are optional.
|
|
506
506
|
*/
|
|
507
|
-
peerDependenciesMeta?: Record<string, {optional: true}
|
|
507
|
+
peerDependenciesMeta?: Partial<Record<string, {optional: true}>>;
|
|
508
508
|
|
|
509
509
|
/**
|
|
510
510
|
Package names that are bundled when the package is published.
|
|
@@ -520,7 +520,7 @@ declare namespace PackageJson {
|
|
|
520
520
|
Engines that this package runs on.
|
|
521
521
|
*/
|
|
522
522
|
engines?: {
|
|
523
|
-
[EngineName in 'npm' | 'node' | string]
|
|
523
|
+
[EngineName in 'npm' | 'node' | string]?: string;
|
|
524
524
|
};
|
|
525
525
|
|
|
526
526
|
/**
|
package/source/partial-deep.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {BuiltIns} from './internal';
|
|
1
|
+
import type {BuiltIns} from './internal';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
Create a type from another type with all keys and nested keys set to optional.
|
|
@@ -9,7 +9,7 @@ Use-cases:
|
|
|
9
9
|
|
|
10
10
|
@example
|
|
11
11
|
```
|
|
12
|
-
import {PartialDeep} from 'type-fest';
|
|
12
|
+
import type {PartialDeep} from 'type-fest';
|
|
13
13
|
|
|
14
14
|
const settings: Settings = {
|
|
15
15
|
textEditor: {
|
package/source/pascal-case.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {CamelCase} from './camel-case';
|
|
1
|
+
import type {CamelCase} from './camel-case';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
Converts a string literal to pascal-case.
|
|
5
5
|
|
|
6
6
|
@example
|
|
7
7
|
```
|
|
8
|
-
import {PascalCase} from 'type-fest';
|
|
8
|
+
import type {PascalCase} from 'type-fest';
|
|
9
9
|
|
|
10
10
|
// Simple
|
|
11
11
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {PascalCase} from './pascal-case';
|
|
1
|
+
import type {PascalCase} from './pascal-case';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
Convert object properties to pascal case recursively.
|
|
@@ -10,6 +10,8 @@ This can be useful when, for example, converting some API types from a different
|
|
|
10
10
|
|
|
11
11
|
@example
|
|
12
12
|
```
|
|
13
|
+
import type {PascalCasedPropertiesDeep} from 'type-fest';
|
|
14
|
+
|
|
13
15
|
interface User {
|
|
14
16
|
userId: number;
|
|
15
17
|
userName: string;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {PascalCase} from './pascal-case';
|
|
1
|
+
import type {PascalCase} from './pascal-case';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
Convert object properties to pascal case but not recursively.
|
|
@@ -10,6 +10,8 @@ This can be useful when, for example, converting some API types from a different
|
|
|
10
10
|
|
|
11
11
|
@example
|
|
12
12
|
```
|
|
13
|
+
import type {PascalCasedProperties} from 'type-fest';
|
|
14
|
+
|
|
13
15
|
interface User {
|
|
14
16
|
userId: number;
|
|
15
17
|
userName: string;
|
package/source/promisable.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/31394)
|
|
|
9
9
|
|
|
10
10
|
@example
|
|
11
11
|
```
|
|
12
|
-
import {Promisable} from 'type-fest';
|
|
12
|
+
import type {Promisable} from 'type-fest';
|
|
13
13
|
|
|
14
14
|
async function logger(getLogEntry: () => Promisable<string>): Promise<void> {
|
|
15
15
|
const entry = await getLogEntry();
|
|
@@ -7,10 +7,10 @@ If the type is not a `Promise`, the type itself is returned.
|
|
|
7
7
|
|
|
8
8
|
@example
|
|
9
9
|
```
|
|
10
|
-
import {PromiseValue} from 'type-fest';
|
|
10
|
+
import type {PromiseValue} from 'type-fest';
|
|
11
11
|
|
|
12
12
|
type AsyncData = Promise<string>;
|
|
13
|
-
let asyncData:
|
|
13
|
+
let asyncData: AsyncData = Promise.resolve('ABC');
|
|
14
14
|
|
|
15
15
|
type Data = PromiseValue<AsyncData>;
|
|
16
16
|
let data: Data = await asyncData;
|
|
@@ -20,8 +20,8 @@ type SyncData = PromiseValue<string>;
|
|
|
20
20
|
let syncData: SyncData = getSyncData();
|
|
21
21
|
|
|
22
22
|
// Here's an example that shows how this type reacts to recursive Promise types.
|
|
23
|
-
type RecursiveAsyncData = Promise<Promise<string
|
|
24
|
-
let recursiveAsyncData: PromiseValue<RecursiveAsyncData> = Promise.resolve(Promise.resolve('ABC'));
|
|
23
|
+
type RecursiveAsyncData = Promise<Promise<string>>;
|
|
24
|
+
let recursiveAsyncData: PromiseValue<RecursiveAsyncData> = await Promise.resolve(Promise.resolve('ABC'));
|
|
25
25
|
```
|
|
26
26
|
|
|
27
27
|
@category Async
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {BuiltIns} from './internal';
|
|
1
|
+
import type {BuiltIns} from './internal';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
Convert `object`s, `Map`s, `Set`s, and `Array`s and all of their keys/elements into immutable structures recursively.
|
|
@@ -15,7 +15,7 @@ Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/13923)
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
// main.ts
|
|
18
|
-
import {ReadonlyDeep} from 'type-fest';
|
|
18
|
+
import type {ReadonlyDeep} from 'type-fest';
|
|
19
19
|
import dataJson = require('./data.json');
|
|
20
20
|
|
|
21
21
|
const data: ReadonlyDeep<typeof dataJson> = dataJson;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Creates a read-only tuple of type `Element` and with the length of `Length`.
|
|
3
|
+
|
|
4
|
+
@private
|
|
5
|
+
@see `ReadonlyTuple` which is safer because it tests if `Length` is a specific finite number.
|
|
6
|
+
*/
|
|
7
|
+
type BuildTupleHelper<Element, Length extends number, Rest extends Element[]> =
|
|
8
|
+
Rest['length'] extends Length ?
|
|
9
|
+
readonly [...Rest] : // Terminate with readonly array (aka tuple)
|
|
10
|
+
BuildTupleHelper<Element, Length, [Element, ...Rest]>;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
Create a type that represents a read-only tuple of the given type and length.
|
|
14
|
+
|
|
15
|
+
Use-cases:
|
|
16
|
+
- Declaring fixed-length tuples with a large number of items.
|
|
17
|
+
- Creating a range union (for example, `0 | 1 | 2 | 3 | 4` from the keys of such a type) without having to resort to recursive types.
|
|
18
|
+
- Creating a tuple of coordinates with a static length, for example, length of 3 for a 3D vector.
|
|
19
|
+
|
|
20
|
+
@example
|
|
21
|
+
```
|
|
22
|
+
import {ReadonlyTuple} from 'type-fest';
|
|
23
|
+
|
|
24
|
+
type FencingTeam = ReadonlyTuple<string, 3>;
|
|
25
|
+
|
|
26
|
+
const guestFencingTeam: FencingTeam = ['Josh', 'Michael', 'Robert'];
|
|
27
|
+
|
|
28
|
+
const homeFencingTeam: FencingTeam = ['George', 'John'];
|
|
29
|
+
//=> error TS2322: Type string[] is not assignable to type 'FencingTeam'
|
|
30
|
+
|
|
31
|
+
guestFencingTeam.push('Sam');
|
|
32
|
+
//=> error TS2339: Property 'push' does not exist on type 'FencingTeam'
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
@category Utilities
|
|
36
|
+
*/
|
|
37
|
+
export type ReadonlyTuple<Element, Length extends number> =
|
|
38
|
+
number extends Length
|
|
39
|
+
// Because `Length extends number` and `number extends Length`, then `Length` is not a specific finite number.
|
|
40
|
+
? readonly Element[] // It's not fixed length.
|
|
41
|
+
: BuildTupleHelper<Element, Length, []>; // Otherwise it is a fixed length tuple.
|
|
@@ -34,15 +34,19 @@ type Keyed = {} extends Record<'foo' | 'bar', unknown>
|
|
|
34
34
|
Using a [mapped type](https://www.typescriptlang.org/docs/handbook/2/mapped-types.html#further-exploration), you can then check for each `KeyType` of `ObjectType`...
|
|
35
35
|
|
|
36
36
|
```
|
|
37
|
+
import type {RemoveIndexSignature} from 'type-fest';
|
|
38
|
+
|
|
37
39
|
type RemoveIndexSignature<ObjectType> = {
|
|
38
40
|
[KeyType in keyof ObjectType // Map each key of `ObjectType`...
|
|
39
|
-
|
|
41
|
+
]: ObjectType[KeyType]; // ...to its original value, i.e. `RemoveIndexSignature<Foo> == Foo`.
|
|
40
42
|
};
|
|
41
43
|
```
|
|
42
44
|
|
|
43
45
|
...whether an empty object (`{}`) would be assignable to an object with that `KeyType` (`Record<KeyType, unknown>`)...
|
|
44
46
|
|
|
45
47
|
```
|
|
48
|
+
import type {RemoveIndexSignature} from 'type-fest';
|
|
49
|
+
|
|
46
50
|
type RemoveIndexSignature<ObjectType> = {
|
|
47
51
|
[KeyType in keyof ObjectType
|
|
48
52
|
// Is `{}` assignable to `Record<KeyType, unknown>`?
|
|
@@ -56,6 +60,8 @@ type RemoveIndexSignature<ObjectType> = {
|
|
|
56
60
|
If `{}` is assignable, it means that `KeyType` is an index signature and we want to remove it. If it is not assignable, `KeyType` is a "real" key and we want to keep it.
|
|
57
61
|
|
|
58
62
|
```
|
|
63
|
+
import type {RemoveIndexSignature} from 'type-fest';
|
|
64
|
+
|
|
59
65
|
type RemoveIndexSignature<ObjectType> = {
|
|
60
66
|
[KeyType in keyof ObjectType
|
|
61
67
|
as {} extends Record<KeyType, unknown>
|
|
@@ -67,7 +73,7 @@ type RemoveIndexSignature<ObjectType> = {
|
|
|
67
73
|
|
|
68
74
|
@example
|
|
69
75
|
```
|
|
70
|
-
import {RemoveIndexSignature} from 'type-fest';
|
|
76
|
+
import type {RemoveIndexSignature} from 'type-fest';
|
|
71
77
|
|
|
72
78
|
interface Example {
|
|
73
79
|
// These index signatures will be removed.
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
type ReplaceOptions = {
|
|
2
|
+
all?: boolean;
|
|
3
|
+
};
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
Represents a string with some or all matches replaced by a replacement.
|
|
7
|
+
|
|
8
|
+
Use-case:
|
|
9
|
+
- `snake-case-path` to `dotted.path.notation`
|
|
10
|
+
- Changing date/time format: `01-08-2042` → `01/08/2042`
|
|
11
|
+
- Manipulation of type properties, for example, removal of prefixes
|
|
12
|
+
|
|
13
|
+
@example
|
|
14
|
+
```
|
|
15
|
+
import {Replace} from 'type-fest';
|
|
16
|
+
|
|
17
|
+
declare function replace<
|
|
18
|
+
Input extends string,
|
|
19
|
+
Search extends string,
|
|
20
|
+
Replacement extends string
|
|
21
|
+
>(
|
|
22
|
+
input: Input,
|
|
23
|
+
search: Search,
|
|
24
|
+
replacement: Replacement
|
|
25
|
+
): Replace<Input, Search, Replacement>;
|
|
26
|
+
|
|
27
|
+
declare function replaceAll<
|
|
28
|
+
Input extends string,
|
|
29
|
+
Search extends string,
|
|
30
|
+
Replacement extends string
|
|
31
|
+
>(
|
|
32
|
+
input: Input,
|
|
33
|
+
search: Search,
|
|
34
|
+
replacement: Replacement
|
|
35
|
+
): Replace<Input, Search, Replacement, {all: true}>;
|
|
36
|
+
|
|
37
|
+
// The return type is the exact string literal, not just `string`.
|
|
38
|
+
|
|
39
|
+
replace('hello ?', '?', '🦄');
|
|
40
|
+
//=> 'hello 🦄'
|
|
41
|
+
|
|
42
|
+
replace('hello ??', '?', '❓');
|
|
43
|
+
//=> 'hello ❓?'
|
|
44
|
+
|
|
45
|
+
replaceAll('10:42:00', ':', '-');
|
|
46
|
+
//=> '10-42-00'
|
|
47
|
+
|
|
48
|
+
replaceAll('__userName__', '__', '');
|
|
49
|
+
//=> 'userName'
|
|
50
|
+
|
|
51
|
+
replaceAll('My Cool Title', ' ', '');
|
|
52
|
+
//=> 'MyCoolTitle'
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
@category String
|
|
56
|
+
@category Template literal
|
|
57
|
+
*/
|
|
58
|
+
export type Replace<
|
|
59
|
+
Input extends string,
|
|
60
|
+
Search extends string,
|
|
61
|
+
Replacement extends string,
|
|
62
|
+
Options extends ReplaceOptions = {},
|
|
63
|
+
> = Input extends `${infer Head}${Search}${infer Tail}`
|
|
64
|
+
? Options['all'] extends true
|
|
65
|
+
? Replace<`${Head}${Replacement}${Tail}`, Search, Replacement, Options>
|
|
66
|
+
: `${Head}${Replacement}${Tail}`
|
|
67
|
+
: Input;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {Except} from './except';
|
|
1
|
+
import type {Except} from './except';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
Create a type that requires at least one of the given keys. The remaining keys are kept as is.
|
|
5
5
|
|
|
6
6
|
@example
|
|
7
7
|
```
|
|
8
|
-
import {RequireAtLeastOne} from 'type-fest';
|
|
8
|
+
import type {RequireAtLeastOne} from 'type-fest';
|
|
9
9
|
|
|
10
10
|
type Responder = {
|
|
11
11
|
text?: () => string;
|
package/source/schema.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {SplitIncludingDelimiters} from './delimiter-case';
|
|
2
|
-
import {SnakeCase} from './snake-case';
|
|
3
|
-
import {Includes} from './includes';
|
|
1
|
+
import type {SplitIncludingDelimiters} from './delimiter-case';
|
|
2
|
+
import type {SnakeCase} from './snake-case';
|
|
3
|
+
import type {Includes} from './includes';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
Returns a boolean for whether the string is screaming snake case.
|
|
@@ -18,7 +18,7 @@ This can be useful when, for example, converting a camel-cased object property t
|
|
|
18
18
|
|
|
19
19
|
@example
|
|
20
20
|
```
|
|
21
|
-
import {ScreamingSnakeCase} from 'type-fest';
|
|
21
|
+
import type {ScreamingSnakeCase} from 'type-fest';
|
|
22
22
|
|
|
23
23
|
const someVariable: ScreamingSnakeCase<'fooBar'> = 'FOO_BAR';
|
|
24
24
|
```
|
package/source/set-optional.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {Except} from './except';
|
|
2
|
-
import {Simplify} from './simplify';
|
|
1
|
+
import type {Except} from './except';
|
|
2
|
+
import type {Simplify} from './simplify';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
Create a type that makes the given keys optional. The remaining keys are kept as is. The sister of the `SetRequired` type.
|
|
@@ -8,7 +8,7 @@ Use-case: You want to define a single model where the only thing that changes is
|
|
|
8
8
|
|
|
9
9
|
@example
|
|
10
10
|
```
|
|
11
|
-
import {SetOptional} from 'type-fest';
|
|
11
|
+
import type {SetOptional} from 'type-fest';
|
|
12
12
|
|
|
13
13
|
type Foo = {
|
|
14
14
|
a: number;
|
package/source/set-required.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {Except} from './except';
|
|
2
|
-
import {Simplify} from './simplify';
|
|
1
|
+
import type {Except} from './except';
|
|
2
|
+
import type {Simplify} from './simplify';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
Create a type that makes the given keys required. The remaining keys are kept as is. The sister of the `SetOptional` type.
|
|
@@ -8,7 +8,7 @@ Use-case: You want to define a single model where the only thing that changes is
|
|
|
8
8
|
|
|
9
9
|
@example
|
|
10
10
|
```
|
|
11
|
-
import {SetRequired} from 'type-fest';
|
|
11
|
+
import type {SetRequired} from 'type-fest';
|
|
12
12
|
|
|
13
13
|
type Foo = {
|
|
14
14
|
a?: number;
|
|
@@ -9,7 +9,7 @@ Use-case: You want to define a wrapped function that returns something different
|
|
|
9
9
|
|
|
10
10
|
@example
|
|
11
11
|
```
|
|
12
|
-
import {SetReturnType} from 'type-fest';
|
|
12
|
+
import type {SetReturnType} from 'type-fest';
|
|
13
13
|
|
|
14
14
|
type MyFunctionThatCanThrow = (foo: SomeType, bar: unknown) => SomeOtherType;
|
|
15
15
|
|
package/source/simplify.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ Useful to flatten the type output to improve type hints shown in editors. And al
|
|
|
3
3
|
|
|
4
4
|
@example
|
|
5
5
|
```
|
|
6
|
-
import {Simplify} from 'type-fest';
|
|
6
|
+
import type {Simplify} from 'type-fest';
|
|
7
7
|
|
|
8
8
|
type PositionProps = {
|
|
9
9
|
top: number;
|
|
@@ -25,7 +25,7 @@ If the type definition must be an interface (perhaps it was defined in a third-p
|
|
|
25
25
|
|
|
26
26
|
@example
|
|
27
27
|
```
|
|
28
|
-
import {Simplify} from 'type-fest';
|
|
28
|
+
import type {Simplify} from 'type-fest';
|
|
29
29
|
|
|
30
30
|
interface SomeInterface {
|
|
31
31
|
foo: number;
|