type-fest 3.6.1 → 3.7.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 +8 -0
- package/package.json +8 -3
- package/readme.md +8 -0
- package/source/basic.d.ts +14 -0
- package/source/except.d.ts +28 -5
- package/source/internal.d.ts +5 -0
- package/source/is-literal.d.ts +252 -0
- package/source/tagged-union.d.ts +51 -0
- package/source/tsconfig-json.d.ts +64 -4
package/index.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export * from './source/observable-like';
|
|
|
7
7
|
// Utilities
|
|
8
8
|
export type {EmptyObject, IsEmptyObject} from './source/empty-object';
|
|
9
9
|
export type {Except} from './source/except';
|
|
10
|
+
export type {TaggedUnion} from './source/tagged-union';
|
|
10
11
|
export type {Writable} from './source/writable';
|
|
11
12
|
export type {WritableDeep} from './source/writable-deep';
|
|
12
13
|
export type {Merge} from './source/merge';
|
|
@@ -76,6 +77,13 @@ export type {HasRequiredKeys} from './source/has-required-keys';
|
|
|
76
77
|
export type {Spread} from './source/spread';
|
|
77
78
|
export type {TupleToUnion} from './source/tuple-to-union';
|
|
78
79
|
export type {IsEqual} from './source/is-equal';
|
|
80
|
+
export type {
|
|
81
|
+
IsLiteral,
|
|
82
|
+
IsStringLiteral,
|
|
83
|
+
IsNumericLiteral,
|
|
84
|
+
IsBooleanLiteral,
|
|
85
|
+
IsSymbolLiteral,
|
|
86
|
+
} from './source/is-literal';
|
|
79
87
|
|
|
80
88
|
// Template literal types
|
|
81
89
|
export type {CamelCase} from './source/camel-case';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "type-fest",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.7.0",
|
|
4
4
|
"description": "A collection of essential TypeScript types",
|
|
5
5
|
"license": "(MIT OR CC0-1.0)",
|
|
6
6
|
"repository": "sindresorhus/type-fest",
|
|
@@ -36,8 +36,8 @@
|
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@sindresorhus/tsconfig": "~0.7.0",
|
|
38
38
|
"expect-type": "^0.15.0",
|
|
39
|
-
"tsd": "^0.
|
|
40
|
-
"typescript": "^
|
|
39
|
+
"tsd": "^0.28.0",
|
|
40
|
+
"typescript": "^5.0.2",
|
|
41
41
|
"xo": "^0.53.1"
|
|
42
42
|
},
|
|
43
43
|
"xo": {
|
|
@@ -49,5 +49,10 @@
|
|
|
49
49
|
"@typescript-eslint/no-redeclare": "off",
|
|
50
50
|
"@typescript-eslint/no-confusing-void-expression": "off"
|
|
51
51
|
}
|
|
52
|
+
},
|
|
53
|
+
"tsd": {
|
|
54
|
+
"compilerOptions": {
|
|
55
|
+
"noUnusedLocals": false
|
|
56
|
+
}
|
|
52
57
|
}
|
|
53
58
|
}
|
package/readme.md
CHANGED
|
@@ -120,6 +120,8 @@ Click the type names for complete docs.
|
|
|
120
120
|
- [`Primitive`](source/primitive.d.ts) - Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
|
|
121
121
|
- [`Class`](source/basic.d.ts) - Matches a [`class`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
|
|
122
122
|
- [`Constructor`](source/basic.d.ts) - Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
|
|
123
|
+
- [`AbstractClass`](source/basic.d.ts) - Matches an [`abstract class`](https://www.typescriptlang.org/docs/handbook/classes.html#abstract-classes).
|
|
124
|
+
- [`AbstractConstructor`](source/basic.d.ts) - Matches an [`abstract class`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-2.html#abstract-construct-signatures) constructor.
|
|
123
125
|
- [`TypedArray`](source/typed-array.d.ts) - Matches any [typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray), like `Uint8Array` or `Float64Array`.
|
|
124
126
|
- [`ObservableLike`](source/observable-like.d.ts) - Matches a value that is like an [Observable](https://github.com/tc39/proposal-observable).
|
|
125
127
|
|
|
@@ -171,6 +173,12 @@ Click the type names for complete docs.
|
|
|
171
173
|
- [`HasRequiredKeys`](source/has-required-keys.d.ts) - Create a `true`/`false` type depending on whether the given type has any required fields.
|
|
172
174
|
- [`Spread`](source/spread.d.ts) - Mimic the type inferred by TypeScript when merging two objects or two arrays/tuples using the spread syntax.
|
|
173
175
|
- [`IsEqual`](source/is-equal.d.ts) - Returns a boolean for whether the two given types are equal.
|
|
176
|
+
- [`IsLiteral`](source/is-literal.d.ts) - Returns a boolean for whether the given type is a [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types).
|
|
177
|
+
- [`IsStringLiteral`](source/is-literal.d.ts) - Returns a boolean for whether the given type is a `string` [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types).
|
|
178
|
+
- [`IsNumericLiteral`](source/is-literal.d.ts) - Returns a boolean for whether the given type is a `number` or `bigint` [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types).
|
|
179
|
+
- [`IsBooleanLiteral`](source/is-literal.d.ts) - Returns a boolean for whether the given type is a `true` or `false` [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types).
|
|
180
|
+
- [`IsSymbolLiteral`](source/is-literal.d.ts) - Returns a boolean for whether the given type is a `symbol` [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types).
|
|
181
|
+
- [`TaggedUnion`](source/tagged-union.d.ts) - Create a union of types that share a common discriminant property.
|
|
174
182
|
|
|
175
183
|
### JSON
|
|
176
184
|
|
package/source/basic.d.ts
CHANGED
|
@@ -12,6 +12,20 @@ Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/Jav
|
|
|
12
12
|
*/
|
|
13
13
|
export type Constructor<T, Arguments extends unknown[] = any[]> = new(...arguments_: Arguments) => T;
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
Matches an [`abstract class`](https://www.typescriptlang.org/docs/handbook/classes.html#abstract-classes).
|
|
17
|
+
|
|
18
|
+
@category Class
|
|
19
|
+
*/
|
|
20
|
+
export type AbstractClass<T, Arguments extends unknown[] = any[]> = AbstractConstructor<T, Arguments> & {prototype: T};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
Matches an [`abstract class`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-2.html#abstract-construct-signatures) constructor.
|
|
24
|
+
|
|
25
|
+
@category Class
|
|
26
|
+
*/
|
|
27
|
+
export type AbstractConstructor<T, Arguments extends unknown[] = any[]> = abstract new(...arguments_: Arguments) => T;
|
|
28
|
+
|
|
15
29
|
/**
|
|
16
30
|
Matches a JSON object.
|
|
17
31
|
|
package/source/except.d.ts
CHANGED
|
@@ -29,9 +29,22 @@ type Filtered = Filter<'bar', 'foo'>;
|
|
|
29
29
|
*/
|
|
30
30
|
type Filter<KeyType, ExcludeType> = IsEqual<KeyType, ExcludeType> extends true ? never : (KeyType extends ExcludeType ? never : KeyType);
|
|
31
31
|
|
|
32
|
+
type ExceptOptions = {
|
|
33
|
+
/**
|
|
34
|
+
Disallow assigning non-specified properties.
|
|
35
|
+
|
|
36
|
+
Note that any omitted properties in the resulting type will be present in autocomplete as `undefined`.
|
|
37
|
+
|
|
38
|
+
@default false
|
|
39
|
+
*/
|
|
40
|
+
requireExactProps?: boolean;
|
|
41
|
+
};
|
|
42
|
+
|
|
32
43
|
/**
|
|
33
44
|
Create a type from an object type without certain keys.
|
|
34
45
|
|
|
46
|
+
We recommend setting the `requireExactProps` option to `true`.
|
|
47
|
+
|
|
35
48
|
This type is a stricter version of [`Omit`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-5.html#the-omit-helper-type). The `Omit` type does not restrict the omitted keys to be keys present on the given type, while `Except` does. The benefits of a stricter type are avoiding typos and allowing the compiler to pick up on rename refactors automatically.
|
|
36
49
|
|
|
37
50
|
This type was proposed to the TypeScript team, which declined it, saying they prefer that libraries implement stricter versions of the built-in types ([microsoft/TypeScript#30825](https://github.com/microsoft/TypeScript/issues/30825#issuecomment-523668235)).
|
|
@@ -43,15 +56,25 @@ import type {Except} from 'type-fest';
|
|
|
43
56
|
type Foo = {
|
|
44
57
|
a: number;
|
|
45
58
|
b: string;
|
|
46
|
-
c: boolean;
|
|
47
59
|
};
|
|
48
60
|
|
|
49
|
-
type FooWithoutA = Except<Foo, 'a'
|
|
50
|
-
//=> {b: string}
|
|
61
|
+
type FooWithoutA = Except<Foo, 'a'>;
|
|
62
|
+
//=> {b: string}
|
|
63
|
+
|
|
64
|
+
const fooWithoutA: FooWithoutA = {a: 1, b: '2'};
|
|
65
|
+
//=> errors: 'a' does not exist in type '{ b: string; }'
|
|
66
|
+
|
|
67
|
+
type FooWithoutB = Except<Foo, 'b', {requireExactProps: true}>;
|
|
68
|
+
//=> {a: number} & Partial<Record<"b", never>>
|
|
69
|
+
|
|
70
|
+
const fooWithoutB: FooWithoutB = {a: 1, b: '2'};
|
|
71
|
+
//=> errors at 'b': Type 'string' is not assignable to type 'undefined'.
|
|
51
72
|
```
|
|
52
73
|
|
|
53
74
|
@category Object
|
|
54
75
|
*/
|
|
55
|
-
export type Except<ObjectType, KeysType extends keyof ObjectType> = {
|
|
76
|
+
export type Except<ObjectType, KeysType extends keyof ObjectType, Options extends ExceptOptions = {requireExactProps: false}> = {
|
|
56
77
|
[KeyType in keyof ObjectType as Filter<KeyType, KeysType>]: ObjectType[KeyType];
|
|
57
|
-
}
|
|
78
|
+
} & (Options['requireExactProps'] extends true
|
|
79
|
+
? Partial<Record<KeysType, never>>
|
|
80
|
+
: {});
|
package/source/internal.d.ts
CHANGED
|
@@ -257,3 +257,8 @@ export type HasMultipleCallSignatures<T extends (...arguments: any[]) => unknown
|
|
|
257
257
|
? false
|
|
258
258
|
: true
|
|
259
259
|
: false;
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
Returns a boolean for whether the given `boolean` is not `false`.
|
|
263
|
+
*/
|
|
264
|
+
export type IsNotFalse<T extends boolean> = [T] extends [false] ? false : true;
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import type {Primitive} from './primitive';
|
|
2
|
+
import type {Numeric} from './numeric';
|
|
3
|
+
import type {IsNever, IsNotFalse} from './internal';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
Returns a boolean for whether the given type `T` is the specified `LiteralType`.
|
|
7
|
+
|
|
8
|
+
@link https://stackoverflow.com/a/52806744/10292952
|
|
9
|
+
|
|
10
|
+
@example
|
|
11
|
+
```
|
|
12
|
+
LiteralCheck<1, number>
|
|
13
|
+
//=> true
|
|
14
|
+
|
|
15
|
+
LiteralCheck<number, number>
|
|
16
|
+
//=> false
|
|
17
|
+
|
|
18
|
+
LiteralCheck<1, string>
|
|
19
|
+
//=> false
|
|
20
|
+
```
|
|
21
|
+
*/
|
|
22
|
+
type LiteralCheck<T, LiteralType extends Primitive> = (
|
|
23
|
+
IsNever<T> extends false // Must be wider than `never`
|
|
24
|
+
? [T] extends [LiteralType] // Must be narrower than `LiteralType`
|
|
25
|
+
? [LiteralType] extends [T] // Cannot be wider than `LiteralType`
|
|
26
|
+
? false
|
|
27
|
+
: true
|
|
28
|
+
: false
|
|
29
|
+
: false
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
Returns a boolean for whether the given type `T` is one of the specified literal types in `LiteralUnionType`.
|
|
34
|
+
|
|
35
|
+
@example
|
|
36
|
+
```
|
|
37
|
+
LiteralChecks<1, Numeric>
|
|
38
|
+
//=> true
|
|
39
|
+
|
|
40
|
+
LiteralChecks<1n, Numeric>
|
|
41
|
+
//=> true
|
|
42
|
+
|
|
43
|
+
LiteralChecks<bigint, Numeric>
|
|
44
|
+
//=> false
|
|
45
|
+
```
|
|
46
|
+
*/
|
|
47
|
+
type LiteralChecks<T, LiteralUnionType> = (
|
|
48
|
+
// Conditional type to force union distribution.
|
|
49
|
+
// If `T` is none of the literal types in the union `LiteralUnionType`, then `LiteralCheck<T, LiteralType>` will evaluate to `false` for the whole union.
|
|
50
|
+
// If `T` is one of the literal types in the union, it will evaluate to `boolean` (i.e. `true | false`)
|
|
51
|
+
IsNotFalse<LiteralUnionType extends Primitive
|
|
52
|
+
? LiteralCheck<T, LiteralUnionType>
|
|
53
|
+
: never
|
|
54
|
+
>
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
Returns a boolean for whether the given type is a `string` [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types).
|
|
59
|
+
|
|
60
|
+
Useful for:
|
|
61
|
+
- providing strongly-typed string manipulation functions
|
|
62
|
+
- constraining strings to be a string literal
|
|
63
|
+
- type utilities, such as when constructing parsers and ASTs
|
|
64
|
+
|
|
65
|
+
@example
|
|
66
|
+
```
|
|
67
|
+
import type {IsStringLiteral} from 'type-fest';
|
|
68
|
+
|
|
69
|
+
type CapitalizedString<T extends string> = IsStringLiteral<T> extends true ? Capitalize<T> : string;
|
|
70
|
+
|
|
71
|
+
// https://github.com/yankeeinlondon/native-dash/blob/master/src/capitalize.ts
|
|
72
|
+
function capitalize<T extends Readonly<string>>(input: T): CapitalizedString<T> {
|
|
73
|
+
return (input.slice(0, 1).toUpperCase() + input.slice(1)) as CapitalizedString<T>;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const output = capitalize('hello, world!');
|
|
77
|
+
//=> 'Hello, world!'
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
@category Utilities
|
|
81
|
+
@category Type Guard
|
|
82
|
+
*/
|
|
83
|
+
export type IsStringLiteral<T> = LiteralCheck<T, string>;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
Returns a boolean for whether the given type is a `number` or `bigint` [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types).
|
|
87
|
+
|
|
88
|
+
Useful for:
|
|
89
|
+
- providing strongly-typed functions when given literal arguments
|
|
90
|
+
- type utilities, such as when constructing parsers and ASTs
|
|
91
|
+
|
|
92
|
+
@example
|
|
93
|
+
```
|
|
94
|
+
import type {IsNumericLiteral} from 'type-fest';
|
|
95
|
+
|
|
96
|
+
// https://github.com/inocan-group/inferred-types/blob/master/src/types/boolean-logic/EndsWith.ts
|
|
97
|
+
type EndsWith<TValue, TEndsWith extends string> =
|
|
98
|
+
TValue extends string
|
|
99
|
+
? IsStringLiteral<TEndsWith> extends true
|
|
100
|
+
? IsStringLiteral<TValue> extends true
|
|
101
|
+
? TValue extends `${string}${TEndsWith}`
|
|
102
|
+
? true
|
|
103
|
+
: false
|
|
104
|
+
: boolean
|
|
105
|
+
: boolean
|
|
106
|
+
: TValue extends number
|
|
107
|
+
? IsNumericLiteral<TValue> extends true
|
|
108
|
+
? EndsWith<`${TValue}`, TEndsWith>
|
|
109
|
+
: false
|
|
110
|
+
: false;
|
|
111
|
+
|
|
112
|
+
function endsWith<Input extends string | number, End extends string>(input: Input, end: End) {
|
|
113
|
+
return `${input}`.endsWith(end) as EndsWith<Input, End>;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
endsWith('abc', 'c');
|
|
117
|
+
//=> true
|
|
118
|
+
|
|
119
|
+
endsWith(123456, '456');
|
|
120
|
+
//=> true
|
|
121
|
+
|
|
122
|
+
const end = '123' as string;
|
|
123
|
+
|
|
124
|
+
endsWith('abc123', end);
|
|
125
|
+
//=> boolean
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
@category Utilities
|
|
129
|
+
@category Type Guard
|
|
130
|
+
*/
|
|
131
|
+
export type IsNumericLiteral<T> = LiteralChecks<T, Numeric>;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
Returns a boolean for whether the given type is a `true` or `false` [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types).
|
|
135
|
+
|
|
136
|
+
Useful for:
|
|
137
|
+
- providing strongly-typed functions when given literal arguments
|
|
138
|
+
- type utilities, such as when constructing parsers and ASTs
|
|
139
|
+
|
|
140
|
+
@example
|
|
141
|
+
```
|
|
142
|
+
import type {IsBooleanLiteral} from 'type-fest';
|
|
143
|
+
|
|
144
|
+
const id = 123;
|
|
145
|
+
|
|
146
|
+
type GetId<AsString extends boolean> =
|
|
147
|
+
IsBooleanLiteral<AsString> extends true
|
|
148
|
+
? AsString extends true
|
|
149
|
+
? `${typeof id}`
|
|
150
|
+
: typeof id
|
|
151
|
+
: number | string;
|
|
152
|
+
|
|
153
|
+
function getId<AsString extends boolean = false>(options?: {asString: AsString}) {
|
|
154
|
+
return (options?.asString ? `${id}` : id) as GetId<AsString>;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const numberId = getId();
|
|
158
|
+
//=> 123
|
|
159
|
+
|
|
160
|
+
const stringId = getId({asString: true});
|
|
161
|
+
//=> '123'
|
|
162
|
+
|
|
163
|
+
declare const runtimeBoolean: boolean;
|
|
164
|
+
const eitherId = getId({asString: runtimeBoolean});
|
|
165
|
+
//=> number | string
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
@category Utilities
|
|
169
|
+
@category Type Guard
|
|
170
|
+
*/
|
|
171
|
+
export type IsBooleanLiteral<T> = LiteralCheck<T, boolean>;
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
Returns a boolean for whether the given type is a `symbol` [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types).
|
|
175
|
+
|
|
176
|
+
Useful for:
|
|
177
|
+
- providing strongly-typed functions when given literal arguments
|
|
178
|
+
- type utilities, such as when constructing parsers and ASTs
|
|
179
|
+
|
|
180
|
+
@example
|
|
181
|
+
```
|
|
182
|
+
import type {IsSymbolLiteral} from 'type-fest';
|
|
183
|
+
|
|
184
|
+
type Get<Obj extends Record<symbol, number>, Key extends keyof Obj> =
|
|
185
|
+
IsSymbolLiteral<Key> extends true
|
|
186
|
+
? Obj[Key]
|
|
187
|
+
: number;
|
|
188
|
+
|
|
189
|
+
function get<Obj extends Record<symbol, number>, Key extends keyof Obj>(o: Obj, key: Key) {
|
|
190
|
+
return o[key] as Get<Obj, Key>;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const symbolLiteral = Symbol('literal');
|
|
194
|
+
const symbolValue: symbol = Symbol('value');
|
|
195
|
+
|
|
196
|
+
get({[symbolLiteral]: 1} as const, symbolLiteral);
|
|
197
|
+
//=> 1
|
|
198
|
+
|
|
199
|
+
get({[symbolValue]: 1} as const, symbolValue);
|
|
200
|
+
//=> number
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
@category Utilities
|
|
204
|
+
@category Type Guard
|
|
205
|
+
*/
|
|
206
|
+
export type IsSymbolLiteral<T> = LiteralCheck<T, symbol>;
|
|
207
|
+
|
|
208
|
+
/** Helper type for `IsLiteral`. */
|
|
209
|
+
type IsLiteralUnion<T> =
|
|
210
|
+
| IsStringLiteral<T>
|
|
211
|
+
| IsNumericLiteral<T>
|
|
212
|
+
| IsBooleanLiteral<T>
|
|
213
|
+
| IsSymbolLiteral<T>;
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
Returns a boolean for whether the given type is a [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types).
|
|
217
|
+
|
|
218
|
+
Useful for:
|
|
219
|
+
- providing strongly-typed functions when given literal arguments
|
|
220
|
+
- type utilities, such as when constructing parsers and ASTs
|
|
221
|
+
|
|
222
|
+
@example
|
|
223
|
+
```
|
|
224
|
+
import type {IsLiteral} from 'type-fest';
|
|
225
|
+
|
|
226
|
+
// https://github.com/inocan-group/inferred-types/blob/master/src/types/string-literals/StripLeading.ts
|
|
227
|
+
export type StripLeading<A, B> =
|
|
228
|
+
A extends string
|
|
229
|
+
? B extends string
|
|
230
|
+
? IsLiteral<A> extends true
|
|
231
|
+
? string extends B ? never : A extends `${B & string}${infer After}` ? After : A
|
|
232
|
+
: string
|
|
233
|
+
: A
|
|
234
|
+
: A;
|
|
235
|
+
|
|
236
|
+
function stripLeading<Input extends string, Strip extends string>(input: Input, strip: Strip) {
|
|
237
|
+
return input.replace(`^${strip}`, '') as StripLeading<Input, Strip>;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
stripLeading('abc123', 'abc');
|
|
241
|
+
//=> '123'
|
|
242
|
+
|
|
243
|
+
const str = 'abc123' as string;
|
|
244
|
+
|
|
245
|
+
stripLeading(str, 'abc');
|
|
246
|
+
//=> string
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
@category Utilities
|
|
250
|
+
@category Type Guard
|
|
251
|
+
*/
|
|
252
|
+
export type IsLiteral<T extends Primitive> = IsNotFalse<IsLiteralUnion<T>>;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Create a union of types that share a common discriminant property.
|
|
3
|
+
|
|
4
|
+
Use-case: A shorter way to declare tagged unions with multiple members.
|
|
5
|
+
|
|
6
|
+
@example
|
|
7
|
+
```
|
|
8
|
+
import type {TaggedUnion} from 'type-fest';
|
|
9
|
+
|
|
10
|
+
type Tagged<Fields extends Record<string, unknown> = TaggedUnion<'type', Fields>
|
|
11
|
+
|
|
12
|
+
// The TaggedUnion utility reduces the amount of boilerplate needed to create a tagged union with multiple members, making the code more concise.
|
|
13
|
+
type EventMessage = Tagged<{
|
|
14
|
+
OpenExternalUrl: {
|
|
15
|
+
url: string;
|
|
16
|
+
id: number;
|
|
17
|
+
language: string;
|
|
18
|
+
};
|
|
19
|
+
ToggleBackButtonVisibility: {
|
|
20
|
+
visible: boolean;
|
|
21
|
+
};
|
|
22
|
+
PurchaseButtonPressed: {
|
|
23
|
+
price: number;
|
|
24
|
+
time: Date;
|
|
25
|
+
};
|
|
26
|
+
NavigationStateChanged: {
|
|
27
|
+
navigation?: string;
|
|
28
|
+
};
|
|
29
|
+
}>;
|
|
30
|
+
|
|
31
|
+
// Here is the same type created without this utility.
|
|
32
|
+
type EventMessage =
|
|
33
|
+
| {
|
|
34
|
+
type: 'OpenExternalUrl';
|
|
35
|
+
url: string;
|
|
36
|
+
id: number;
|
|
37
|
+
language: string;
|
|
38
|
+
}
|
|
39
|
+
| {type: 'ToggleBackButtonVisibility'; visible: boolean}
|
|
40
|
+
| {type: 'PurchaseButtonPressed'; price: number; time: Date}
|
|
41
|
+
| {type: 'NavigationStateChanged'; navigation?: string};
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
@category Utilities
|
|
45
|
+
*/
|
|
46
|
+
export type TaggedUnion<
|
|
47
|
+
TagKey extends string,
|
|
48
|
+
UnionMembers extends Record<string, Record<string, unknown>>,
|
|
49
|
+
> = {
|
|
50
|
+
[Name in keyof UnionMembers]: {[Key in TagKey]: Name} & UnionMembers[Name];
|
|
51
|
+
}[keyof UnionMembers];
|
|
@@ -225,18 +225,24 @@ declare namespace TsConfigJson {
|
|
|
225
225
|
export type ModuleResolution =
|
|
226
226
|
| 'classic'
|
|
227
227
|
| 'node'
|
|
228
|
+
| 'node10'
|
|
228
229
|
| 'node16'
|
|
229
230
|
| 'nodenext'
|
|
231
|
+
| 'bundler'
|
|
230
232
|
// Pascal-cased alternatives
|
|
231
233
|
| 'Classic'
|
|
232
234
|
| 'Node'
|
|
235
|
+
| 'Node10'
|
|
233
236
|
| 'Node16'
|
|
234
|
-
| 'NodeNext'
|
|
237
|
+
| 'NodeNext'
|
|
238
|
+
| 'Bundler';
|
|
235
239
|
|
|
236
240
|
export type ModuleDetection =
|
|
237
241
|
| 'auto'
|
|
238
242
|
| 'legacy'
|
|
239
243
|
| 'force';
|
|
244
|
+
|
|
245
|
+
export type IgnoreDeprecations = '5.0';
|
|
240
246
|
}
|
|
241
247
|
|
|
242
248
|
export type CompilerOptions = {
|
|
@@ -244,6 +250,7 @@ declare namespace TsConfigJson {
|
|
|
244
250
|
The character set of the input files.
|
|
245
251
|
|
|
246
252
|
@default 'utf8'
|
|
253
|
+
@deprecated This option will be removed in TypeScript 5.5.
|
|
247
254
|
*/
|
|
248
255
|
charset?: string;
|
|
249
256
|
|
|
@@ -402,7 +409,7 @@ declare namespace TsConfigJson {
|
|
|
402
409
|
/**
|
|
403
410
|
Specifies the end of line sequence to be used when emitting files: 'crlf' (Windows) or 'lf' (Unix).
|
|
404
411
|
|
|
405
|
-
|
|
412
|
+
@default 'LF'
|
|
406
413
|
*/
|
|
407
414
|
newLine?: CompilerOptions.NewLine;
|
|
408
415
|
|
|
@@ -473,6 +480,7 @@ declare namespace TsConfigJson {
|
|
|
473
480
|
Disable strict checking of generic signatures in function types.
|
|
474
481
|
|
|
475
482
|
@default false
|
|
483
|
+
@deprecated This option will be removed in TypeScript 5.5.
|
|
476
484
|
*/
|
|
477
485
|
noStrictGenericChecks?: boolean;
|
|
478
486
|
|
|
@@ -563,6 +571,7 @@ declare namespace TsConfigJson {
|
|
|
563
571
|
Suppress excess property checks for object literals.
|
|
564
572
|
|
|
565
573
|
@default false
|
|
574
|
+
@deprecated This option will be removed in TypeScript 5.5.
|
|
566
575
|
*/
|
|
567
576
|
suppressExcessPropertyErrors?: boolean;
|
|
568
577
|
|
|
@@ -570,6 +579,7 @@ declare namespace TsConfigJson {
|
|
|
570
579
|
Suppress noImplicitAny errors for indexing objects lacking index signatures.
|
|
571
580
|
|
|
572
581
|
@default false
|
|
582
|
+
@deprecated This option will be removed in TypeScript 5.5.
|
|
573
583
|
*/
|
|
574
584
|
suppressImplicitAnyIndexErrors?: boolean;
|
|
575
585
|
|
|
@@ -682,7 +692,7 @@ declare namespace TsConfigJson {
|
|
|
682
692
|
/**
|
|
683
693
|
Disallow inconsistently-cased references to the same file.
|
|
684
694
|
|
|
685
|
-
@default
|
|
695
|
+
@default true
|
|
686
696
|
*/
|
|
687
697
|
forceConsistentCasingInFileNames?: boolean;
|
|
688
698
|
|
|
@@ -755,6 +765,7 @@ declare namespace TsConfigJson {
|
|
|
755
765
|
Do not emit `'use strict'` directives in module output.
|
|
756
766
|
|
|
757
767
|
@default false
|
|
768
|
+
@deprecated This option will be removed in TypeScript 5.5.
|
|
758
769
|
*/
|
|
759
770
|
noImplicitUseStrict?: boolean;
|
|
760
771
|
|
|
@@ -802,6 +813,7 @@ declare namespace TsConfigJson {
|
|
|
802
813
|
Specify emit/checking behavior for imports that are only used for types.
|
|
803
814
|
|
|
804
815
|
@default 'remove'
|
|
816
|
+
@deprecated Use `verbatimModuleSyntax` instead.
|
|
805
817
|
*/
|
|
806
818
|
importsNotUsedAsValues?: CompilerOptions.ImportsNotUsedAsValues;
|
|
807
819
|
|
|
@@ -872,6 +884,7 @@ declare namespace TsConfigJson {
|
|
|
872
884
|
Resolve `keyof` to string valued property names only (no numbers or symbols).
|
|
873
885
|
|
|
874
886
|
@default false
|
|
887
|
+
@deprecated This option will be removed in TypeScript 5.5.
|
|
875
888
|
*/
|
|
876
889
|
keyofStringsOnly?: boolean;
|
|
877
890
|
|
|
@@ -942,6 +955,7 @@ declare namespace TsConfigJson {
|
|
|
942
955
|
Preserve unused imported values in the JavaScript output that would otherwise be removed.
|
|
943
956
|
|
|
944
957
|
@default true
|
|
958
|
+
@deprecated Use `verbatimModuleSyntax` instead.
|
|
945
959
|
*/
|
|
946
960
|
preserveValueImports?: boolean;
|
|
947
961
|
|
|
@@ -956,6 +970,51 @@ declare namespace TsConfigJson {
|
|
|
956
970
|
@default 'auto'
|
|
957
971
|
*/
|
|
958
972
|
moduleDetection?: CompilerOptions.ModuleDetection;
|
|
973
|
+
|
|
974
|
+
/**
|
|
975
|
+
Allows TypeScript files to import each other with a TypeScript-specific extension like .ts, .mts, or .tsx.
|
|
976
|
+
|
|
977
|
+
@default false
|
|
978
|
+
*/
|
|
979
|
+
allowImportingTsExtensions?: boolean;
|
|
980
|
+
|
|
981
|
+
/**
|
|
982
|
+
Forces TypeScript to consult the exports field of package.json files if it ever reads from a package in node_modules.
|
|
983
|
+
|
|
984
|
+
@default false
|
|
985
|
+
*/
|
|
986
|
+
resolvePackageJsonExports?: boolean;
|
|
987
|
+
|
|
988
|
+
/**
|
|
989
|
+
Forces TypeScript to consult the imports field of package.json files when performing a lookup that starts with # from a file whose ancestor directory contains a package.json.
|
|
990
|
+
|
|
991
|
+
@default false
|
|
992
|
+
*/
|
|
993
|
+
resolvePackageJsonImports?: boolean;
|
|
994
|
+
|
|
995
|
+
/**
|
|
996
|
+
Suppress errors for file formats that TypeScript does not understand.
|
|
997
|
+
|
|
998
|
+
@default false
|
|
999
|
+
*/
|
|
1000
|
+
allowArbitraryExtensions?: boolean;
|
|
1001
|
+
|
|
1002
|
+
/**
|
|
1003
|
+
List of additional conditions that should succeed when TypeScript resolves from package.json.
|
|
1004
|
+
*/
|
|
1005
|
+
customConditions?: string[];
|
|
1006
|
+
|
|
1007
|
+
/**
|
|
1008
|
+
Anything that uses the type modifier is dropped entirely.
|
|
1009
|
+
|
|
1010
|
+
@default false
|
|
1011
|
+
*/
|
|
1012
|
+
verbatimModuleSyntax?: boolean;
|
|
1013
|
+
|
|
1014
|
+
/**
|
|
1015
|
+
Suppress deprecation warnings
|
|
1016
|
+
*/
|
|
1017
|
+
ignoreDeprecations?: CompilerOptions.IgnoreDeprecations;
|
|
959
1018
|
};
|
|
960
1019
|
|
|
961
1020
|
namespace WatchOptions {
|
|
@@ -1052,6 +1111,7 @@ declare namespace TsConfigJson {
|
|
|
1052
1111
|
True if the output of this reference should be prepended to the output of this project.
|
|
1053
1112
|
|
|
1054
1113
|
Only valid for `--outFile` compilations.
|
|
1114
|
+
@deprecated This option will be removed in TypeScript 5.5.
|
|
1055
1115
|
*/
|
|
1056
1116
|
prepend?: boolean;
|
|
1057
1117
|
|
|
@@ -1091,7 +1151,7 @@ export type TsConfigJson = {
|
|
|
1091
1151
|
/**
|
|
1092
1152
|
Path to base configuration file to inherit from.
|
|
1093
1153
|
*/
|
|
1094
|
-
extends?: string;
|
|
1154
|
+
extends?: string | string[];
|
|
1095
1155
|
|
|
1096
1156
|
/**
|
|
1097
1157
|
If no `files` or `include` property is present in a `tsconfig.json`, the compiler defaults to including all files in the containing directory and subdirectories except those specified by `exclude`. When a `files` property is specified, only those files and those specified by `include` are included.
|