@visulima/object 1.0.7 → 1.0.9
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/CHANGELOG.md +22 -0
- package/README.md +1 -1
- package/dist/index.cjs +187 -1
- package/dist/index.d.cts +147 -116
- package/dist/index.d.mts +147 -116
- package/dist/index.d.ts +147 -116
- package/dist/index.mjs +176 -1
- package/dist/packem_shared/getProperty-BuSUBWTY.cjs +278 -0
- package/dist/packem_shared/getProperty-CqSBQFGO.mjs +269 -0
- package/dist/packem_shared/isPlainObject-BEp1xndO.mjs +12 -0
- package/dist/packem_shared/isPlainObject-D2hOGcuW.cjs +14 -0
- package/dist/packem_shared/omit-Bz46-jv-.mjs +38 -0
- package/dist/packem_shared/omit-DSwyUtN2.cjs +40 -0
- package/dist/packem_shared/paths-are-equal-BvHTDEDJ.cjs +15 -0
- package/dist/packem_shared/paths-are-equal-DY1QWTwZ.mjs +13 -0
- package/dist/packem_shared/pick-BX11mP2m.cjs +52 -0
- package/dist/packem_shared/pick-C9-E3eDJ.mjs +50 -0
- package/package.json +2 -68
- package/dist/packem_shared/getProperty-Crh_HruK.cjs +0 -1
- package/dist/packem_shared/getProperty-D2xQpEOY.mjs +0 -1
- package/dist/packem_shared/isPlainObject-CLTs8VdF.mjs +0 -1
- package/dist/packem_shared/isPlainObject-Ct8-08mJ.cjs +0 -1
- package/dist/packem_shared/omit-BQOPjoaX.cjs +0 -1
- package/dist/packem_shared/omit-BcaD2qRn.mjs +0 -1
- package/dist/packem_shared/paths-are-equal-BgwMgI_D.mjs +0 -1
- package/dist/packem_shared/paths-are-equal-D8teV-Y-.cjs +0 -1
- package/dist/packem_shared/pick-BxgLK1bH.mjs +0 -1
- package/dist/packem_shared/pick-fC8KqJig.cjs +0 -1
package/dist/index.d.mts
CHANGED
|
@@ -19,6 +19,68 @@ declare global {
|
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
/**
|
|
23
|
+
Convert a union type to an intersection type using [distributive conditional types](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
|
|
24
|
+
|
|
25
|
+
Inspired by [this Stack Overflow answer](https://stackoverflow.com/a/50375286/2172153).
|
|
26
|
+
|
|
27
|
+
@example
|
|
28
|
+
```
|
|
29
|
+
import type {UnionToIntersection} from 'type-fest';
|
|
30
|
+
|
|
31
|
+
type Union = {the(): void} | {great(arg: string): void} | {escape: boolean};
|
|
32
|
+
|
|
33
|
+
type Intersection = UnionToIntersection<Union>;
|
|
34
|
+
//=> {the(): void; great(arg: string): void; escape: boolean};
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
A more applicable example which could make its way into your library code follows.
|
|
38
|
+
|
|
39
|
+
@example
|
|
40
|
+
```
|
|
41
|
+
import type {UnionToIntersection} from 'type-fest';
|
|
42
|
+
|
|
43
|
+
class CommandOne {
|
|
44
|
+
commands: {
|
|
45
|
+
a1: () => undefined,
|
|
46
|
+
b1: () => undefined,
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
class CommandTwo {
|
|
51
|
+
commands: {
|
|
52
|
+
a2: (argA: string) => undefined,
|
|
53
|
+
b2: (argB: string) => undefined,
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const union = [new CommandOne(), new CommandTwo()].map(instance => instance.commands);
|
|
58
|
+
type Union = typeof union;
|
|
59
|
+
//=> {a1(): void; b1(): void} | {a2(argA: string): void; b2(argB: string): void}
|
|
60
|
+
|
|
61
|
+
type Intersection = UnionToIntersection<Union>;
|
|
62
|
+
//=> {a1(): void; b1(): void; a2(argA: string): void; b2(argB: string): void}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
@category Type
|
|
66
|
+
*/
|
|
67
|
+
type UnionToIntersection<Union> = (
|
|
68
|
+
// `extends unknown` is always going to be the case and is used to convert the
|
|
69
|
+
// `Union` into a [distributive conditional
|
|
70
|
+
// type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
|
|
71
|
+
Union extends unknown
|
|
72
|
+
// The union type is used as the only argument to a function since the union
|
|
73
|
+
// of function arguments is an intersection.
|
|
74
|
+
? (distributedUnion: Union) => void
|
|
75
|
+
// This won't happen.
|
|
76
|
+
: never
|
|
77
|
+
// Infer the `Intersection` type since TypeScript represents the positional
|
|
78
|
+
// arguments of unions of functions as an intersection of the union.
|
|
79
|
+
) extends ((mergedIntersection: infer Intersection) => void)
|
|
80
|
+
// The `& Union` is to allow indexing by the resulting type
|
|
81
|
+
? Intersection & Union
|
|
82
|
+
: never;
|
|
83
|
+
|
|
22
84
|
declare const emptyObjectSymbol: unique symbol;
|
|
23
85
|
|
|
24
86
|
/**
|
|
@@ -76,8 +138,8 @@ type Includes<Value extends readonly any[], Item> =
|
|
|
76
138
|
@category Utilities
|
|
77
139
|
*/
|
|
78
140
|
type IsEqual<A, B> =
|
|
79
|
-
(<G>() => G extends A ? 1 : 2) extends
|
|
80
|
-
(<G>() => G extends B ? 1 : 2)
|
|
141
|
+
(<G>() => G extends A & G | G ? 1 : 2) extends
|
|
142
|
+
(<G>() => G extends B & G | G ? 1 : 2)
|
|
81
143
|
? true
|
|
82
144
|
: false;
|
|
83
145
|
|
|
@@ -166,6 +228,72 @@ fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface`
|
|
|
166
228
|
*/
|
|
167
229
|
type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
|
|
168
230
|
|
|
231
|
+
/**
|
|
232
|
+
Returns a boolean for whether the given type is `never`.
|
|
233
|
+
|
|
234
|
+
@link https://github.com/microsoft/TypeScript/issues/31751#issuecomment-498526919
|
|
235
|
+
@link https://stackoverflow.com/a/53984913/10292952
|
|
236
|
+
@link https://www.zhenghao.io/posts/ts-never
|
|
237
|
+
|
|
238
|
+
Useful in type utilities, such as checking if something does not occur.
|
|
239
|
+
|
|
240
|
+
@example
|
|
241
|
+
```
|
|
242
|
+
import type {IsNever, And} from 'type-fest';
|
|
243
|
+
|
|
244
|
+
// https://github.com/andnp/SimplyTyped/blob/master/src/types/strings.ts
|
|
245
|
+
type AreStringsEqual<A extends string, B extends string> =
|
|
246
|
+
And<
|
|
247
|
+
IsNever<Exclude<A, B>> extends true ? true : false,
|
|
248
|
+
IsNever<Exclude<B, A>> extends true ? true : false
|
|
249
|
+
>;
|
|
250
|
+
|
|
251
|
+
type EndIfEqual<I extends string, O extends string> =
|
|
252
|
+
AreStringsEqual<I, O> extends true
|
|
253
|
+
? never
|
|
254
|
+
: void;
|
|
255
|
+
|
|
256
|
+
function endIfEqual<I extends string, O extends string>(input: I, output: O): EndIfEqual<I, O> {
|
|
257
|
+
if (input === output) {
|
|
258
|
+
process.exit(0);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
endIfEqual('abc', 'abc');
|
|
263
|
+
//=> never
|
|
264
|
+
|
|
265
|
+
endIfEqual('abc', '123');
|
|
266
|
+
//=> void
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
@category Type Guard
|
|
270
|
+
@category Utilities
|
|
271
|
+
*/
|
|
272
|
+
type IsNever<T> = [T] extends [never] ? true : false;
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
An if-else-like type that resolves depending on whether the given type is `never`.
|
|
276
|
+
|
|
277
|
+
@see {@link IsNever}
|
|
278
|
+
|
|
279
|
+
@example
|
|
280
|
+
```
|
|
281
|
+
import type {IfNever} from 'type-fest';
|
|
282
|
+
|
|
283
|
+
type ShouldBeTrue = IfNever<never>;
|
|
284
|
+
//=> true
|
|
285
|
+
|
|
286
|
+
type ShouldBeBar = IfNever<'not never', 'foo', 'bar'>;
|
|
287
|
+
//=> 'bar'
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
@category Type Guard
|
|
291
|
+
@category Utilities
|
|
292
|
+
*/
|
|
293
|
+
type IfNever<T, TypeIfNever = true, TypeIfNotNever = false> = (
|
|
294
|
+
IsNever<T> extends true ? TypeIfNever : TypeIfNotNever
|
|
295
|
+
);
|
|
296
|
+
|
|
169
297
|
/**
|
|
170
298
|
Returns the static, fixed-length portion of the given array, excluding variable-length parts.
|
|
171
299
|
|
|
@@ -227,7 +355,7 @@ T extends readonly [...infer U] ?
|
|
|
227
355
|
/**
|
|
228
356
|
Returns whether the given array `T` is readonly.
|
|
229
357
|
*/
|
|
230
|
-
type IsArrayReadonly<T extends UnknownArray> = T extends unknown[] ? false : true
|
|
358
|
+
type IsArrayReadonly<T extends UnknownArray> = IfNever<T, false, T extends unknown[] ? false : true>;
|
|
231
359
|
|
|
232
360
|
type StringDigit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
|
|
233
361
|
|
|
@@ -324,49 +452,6 @@ type ShouldBeTrue = IsNegative<-1>;
|
|
|
324
452
|
*/
|
|
325
453
|
type IsNegative<T extends Numeric> = T extends Negative<T> ? true : false;
|
|
326
454
|
|
|
327
|
-
/**
|
|
328
|
-
Returns a boolean for whether the given type is `never`.
|
|
329
|
-
|
|
330
|
-
@link https://github.com/microsoft/TypeScript/issues/31751#issuecomment-498526919
|
|
331
|
-
@link https://stackoverflow.com/a/53984913/10292952
|
|
332
|
-
@link https://www.zhenghao.io/posts/ts-never
|
|
333
|
-
|
|
334
|
-
Useful in type utilities, such as checking if something does not occur.
|
|
335
|
-
|
|
336
|
-
@example
|
|
337
|
-
```
|
|
338
|
-
import type {IsNever, And} from 'type-fest';
|
|
339
|
-
|
|
340
|
-
// https://github.com/andnp/SimplyTyped/blob/master/src/types/strings.ts
|
|
341
|
-
type AreStringsEqual<A extends string, B extends string> =
|
|
342
|
-
And<
|
|
343
|
-
IsNever<Exclude<A, B>> extends true ? true : false,
|
|
344
|
-
IsNever<Exclude<B, A>> extends true ? true : false
|
|
345
|
-
>;
|
|
346
|
-
|
|
347
|
-
type EndIfEqual<I extends string, O extends string> =
|
|
348
|
-
AreStringsEqual<I, O> extends true
|
|
349
|
-
? never
|
|
350
|
-
: void;
|
|
351
|
-
|
|
352
|
-
function endIfEqual<I extends string, O extends string>(input: I, output: O): EndIfEqual<I, O> {
|
|
353
|
-
if (input === output) {
|
|
354
|
-
process.exit(0);
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
endIfEqual('abc', 'abc');
|
|
359
|
-
//=> never
|
|
360
|
-
|
|
361
|
-
endIfEqual('abc', '123');
|
|
362
|
-
//=> void
|
|
363
|
-
```
|
|
364
|
-
|
|
365
|
-
@category Type Guard
|
|
366
|
-
@category Utilities
|
|
367
|
-
*/
|
|
368
|
-
type IsNever<T> = [T] extends [never] ? true : false;
|
|
369
|
-
|
|
370
455
|
/**
|
|
371
456
|
Returns a boolean for whether two given types are both true.
|
|
372
457
|
|
|
@@ -537,9 +622,11 @@ If `<Fill>` is not provided, it will default to `unknown`.
|
|
|
537
622
|
|
|
538
623
|
@link https://itnext.io/implementing-arithmetic-within-typescripts-type-system-a1ef140a6f6f
|
|
539
624
|
*/
|
|
540
|
-
type BuildTuple<L extends number, Fill = unknown, T extends readonly unknown[] = []> =
|
|
541
|
-
?
|
|
542
|
-
:
|
|
625
|
+
type BuildTuple<L extends number, Fill = unknown, T extends readonly unknown[] = []> = number extends L
|
|
626
|
+
? Fill[]
|
|
627
|
+
: L extends T['length']
|
|
628
|
+
? T
|
|
629
|
+
: BuildTuple<L, Fill, [...T, Fill]>;
|
|
543
630
|
|
|
544
631
|
/**
|
|
545
632
|
Returns the maximum value from a tuple of integers.
|
|
@@ -1314,68 +1401,6 @@ type InternalPaths<T, Options extends Required<PathsOptions>> =
|
|
|
1314
1401
|
: never
|
|
1315
1402
|
: never;
|
|
1316
1403
|
|
|
1317
|
-
/**
|
|
1318
|
-
Convert a union type to an intersection type using [distributive conditional types](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
|
|
1319
|
-
|
|
1320
|
-
Inspired by [this Stack Overflow answer](https://stackoverflow.com/a/50375286/2172153).
|
|
1321
|
-
|
|
1322
|
-
@example
|
|
1323
|
-
```
|
|
1324
|
-
import type {UnionToIntersection} from 'type-fest';
|
|
1325
|
-
|
|
1326
|
-
type Union = {the(): void} | {great(arg: string): void} | {escape: boolean};
|
|
1327
|
-
|
|
1328
|
-
type Intersection = UnionToIntersection<Union>;
|
|
1329
|
-
//=> {the(): void; great(arg: string): void; escape: boolean};
|
|
1330
|
-
```
|
|
1331
|
-
|
|
1332
|
-
A more applicable example which could make its way into your library code follows.
|
|
1333
|
-
|
|
1334
|
-
@example
|
|
1335
|
-
```
|
|
1336
|
-
import type {UnionToIntersection} from 'type-fest';
|
|
1337
|
-
|
|
1338
|
-
class CommandOne {
|
|
1339
|
-
commands: {
|
|
1340
|
-
a1: () => undefined,
|
|
1341
|
-
b1: () => undefined,
|
|
1342
|
-
}
|
|
1343
|
-
}
|
|
1344
|
-
|
|
1345
|
-
class CommandTwo {
|
|
1346
|
-
commands: {
|
|
1347
|
-
a2: (argA: string) => undefined,
|
|
1348
|
-
b2: (argB: string) => undefined,
|
|
1349
|
-
}
|
|
1350
|
-
}
|
|
1351
|
-
|
|
1352
|
-
const union = [new CommandOne(), new CommandTwo()].map(instance => instance.commands);
|
|
1353
|
-
type Union = typeof union;
|
|
1354
|
-
//=> {a1(): void; b1(): void} | {a2(argA: string): void; b2(argB: string): void}
|
|
1355
|
-
|
|
1356
|
-
type Intersection = UnionToIntersection<Union>;
|
|
1357
|
-
//=> {a1(): void; b1(): void; a2(argA: string): void; b2(argB: string): void}
|
|
1358
|
-
```
|
|
1359
|
-
|
|
1360
|
-
@category Type
|
|
1361
|
-
*/
|
|
1362
|
-
type UnionToIntersection<Union> = (
|
|
1363
|
-
// `extends unknown` is always going to be the case and is used to convert the
|
|
1364
|
-
// `Union` into a [distributive conditional
|
|
1365
|
-
// type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
|
|
1366
|
-
Union extends unknown
|
|
1367
|
-
// The union type is used as the only argument to a function since the union
|
|
1368
|
-
// of function arguments is an intersection.
|
|
1369
|
-
? (distributedUnion: Union) => void
|
|
1370
|
-
// This won't happen.
|
|
1371
|
-
: never
|
|
1372
|
-
// Infer the `Intersection` type since TypeScript represents the positional
|
|
1373
|
-
// arguments of unions of functions as an intersection of the union.
|
|
1374
|
-
) extends ((mergedIntersection: infer Intersection) => void)
|
|
1375
|
-
// The `& Union` is to allow indexing by the resulting type
|
|
1376
|
-
? Intersection & Union
|
|
1377
|
-
: never;
|
|
1378
|
-
|
|
1379
1404
|
/**
|
|
1380
1405
|
Pick properties from a deeply-nested object.
|
|
1381
1406
|
|
|
@@ -2017,11 +2042,17 @@ array = split(items, ',');
|
|
|
2017
2042
|
type Split<
|
|
2018
2043
|
S extends string,
|
|
2019
2044
|
Delimiter extends string,
|
|
2045
|
+
> = SplitHelper<S, Delimiter>;
|
|
2046
|
+
|
|
2047
|
+
type SplitHelper<
|
|
2048
|
+
S extends string,
|
|
2049
|
+
Delimiter extends string,
|
|
2050
|
+
Accumulator extends string[] = [],
|
|
2020
2051
|
> = S extends `${infer Head}${Delimiter}${infer Tail}`
|
|
2021
|
-
?
|
|
2022
|
-
:
|
|
2023
|
-
?
|
|
2024
|
-
: [S];
|
|
2052
|
+
? SplitHelper<Tail, Delimiter, [...Accumulator, Head]>
|
|
2053
|
+
: Delimiter extends ''
|
|
2054
|
+
? Accumulator
|
|
2055
|
+
: [...Accumulator, S];
|
|
2025
2056
|
|
|
2026
2057
|
type GetOptions = {
|
|
2027
2058
|
/**
|
|
@@ -2224,7 +2255,7 @@ type Get<
|
|
|
2224
2255
|
BaseType,
|
|
2225
2256
|
Path extends
|
|
2226
2257
|
| readonly string[]
|
|
2227
|
-
| LiteralStringUnion<ToString<Paths<BaseType, {bracketNotation: false}> | Paths<BaseType, {bracketNotation: true}>>>,
|
|
2258
|
+
| LiteralStringUnion<ToString<Paths<BaseType, {bracketNotation: false; maxRecursionDepth: 2}> | Paths<BaseType, {bracketNotation: true; maxRecursionDepth: 2}>>>,
|
|
2228
2259
|
Options extends GetOptions = {}> =
|
|
2229
2260
|
GetWithPath<BaseType, Path extends string ? ToPath<Path> : Path, Options>;
|
|
2230
2261
|
|
package/dist/index.d.ts
CHANGED
|
@@ -19,6 +19,68 @@ declare global {
|
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
/**
|
|
23
|
+
Convert a union type to an intersection type using [distributive conditional types](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
|
|
24
|
+
|
|
25
|
+
Inspired by [this Stack Overflow answer](https://stackoverflow.com/a/50375286/2172153).
|
|
26
|
+
|
|
27
|
+
@example
|
|
28
|
+
```
|
|
29
|
+
import type {UnionToIntersection} from 'type-fest';
|
|
30
|
+
|
|
31
|
+
type Union = {the(): void} | {great(arg: string): void} | {escape: boolean};
|
|
32
|
+
|
|
33
|
+
type Intersection = UnionToIntersection<Union>;
|
|
34
|
+
//=> {the(): void; great(arg: string): void; escape: boolean};
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
A more applicable example which could make its way into your library code follows.
|
|
38
|
+
|
|
39
|
+
@example
|
|
40
|
+
```
|
|
41
|
+
import type {UnionToIntersection} from 'type-fest';
|
|
42
|
+
|
|
43
|
+
class CommandOne {
|
|
44
|
+
commands: {
|
|
45
|
+
a1: () => undefined,
|
|
46
|
+
b1: () => undefined,
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
class CommandTwo {
|
|
51
|
+
commands: {
|
|
52
|
+
a2: (argA: string) => undefined,
|
|
53
|
+
b2: (argB: string) => undefined,
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const union = [new CommandOne(), new CommandTwo()].map(instance => instance.commands);
|
|
58
|
+
type Union = typeof union;
|
|
59
|
+
//=> {a1(): void; b1(): void} | {a2(argA: string): void; b2(argB: string): void}
|
|
60
|
+
|
|
61
|
+
type Intersection = UnionToIntersection<Union>;
|
|
62
|
+
//=> {a1(): void; b1(): void; a2(argA: string): void; b2(argB: string): void}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
@category Type
|
|
66
|
+
*/
|
|
67
|
+
type UnionToIntersection<Union> = (
|
|
68
|
+
// `extends unknown` is always going to be the case and is used to convert the
|
|
69
|
+
// `Union` into a [distributive conditional
|
|
70
|
+
// type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
|
|
71
|
+
Union extends unknown
|
|
72
|
+
// The union type is used as the only argument to a function since the union
|
|
73
|
+
// of function arguments is an intersection.
|
|
74
|
+
? (distributedUnion: Union) => void
|
|
75
|
+
// This won't happen.
|
|
76
|
+
: never
|
|
77
|
+
// Infer the `Intersection` type since TypeScript represents the positional
|
|
78
|
+
// arguments of unions of functions as an intersection of the union.
|
|
79
|
+
) extends ((mergedIntersection: infer Intersection) => void)
|
|
80
|
+
// The `& Union` is to allow indexing by the resulting type
|
|
81
|
+
? Intersection & Union
|
|
82
|
+
: never;
|
|
83
|
+
|
|
22
84
|
declare const emptyObjectSymbol: unique symbol;
|
|
23
85
|
|
|
24
86
|
/**
|
|
@@ -76,8 +138,8 @@ type Includes<Value extends readonly any[], Item> =
|
|
|
76
138
|
@category Utilities
|
|
77
139
|
*/
|
|
78
140
|
type IsEqual<A, B> =
|
|
79
|
-
(<G>() => G extends A ? 1 : 2) extends
|
|
80
|
-
(<G>() => G extends B ? 1 : 2)
|
|
141
|
+
(<G>() => G extends A & G | G ? 1 : 2) extends
|
|
142
|
+
(<G>() => G extends B & G | G ? 1 : 2)
|
|
81
143
|
? true
|
|
82
144
|
: false;
|
|
83
145
|
|
|
@@ -166,6 +228,72 @@ fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface`
|
|
|
166
228
|
*/
|
|
167
229
|
type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
|
|
168
230
|
|
|
231
|
+
/**
|
|
232
|
+
Returns a boolean for whether the given type is `never`.
|
|
233
|
+
|
|
234
|
+
@link https://github.com/microsoft/TypeScript/issues/31751#issuecomment-498526919
|
|
235
|
+
@link https://stackoverflow.com/a/53984913/10292952
|
|
236
|
+
@link https://www.zhenghao.io/posts/ts-never
|
|
237
|
+
|
|
238
|
+
Useful in type utilities, such as checking if something does not occur.
|
|
239
|
+
|
|
240
|
+
@example
|
|
241
|
+
```
|
|
242
|
+
import type {IsNever, And} from 'type-fest';
|
|
243
|
+
|
|
244
|
+
// https://github.com/andnp/SimplyTyped/blob/master/src/types/strings.ts
|
|
245
|
+
type AreStringsEqual<A extends string, B extends string> =
|
|
246
|
+
And<
|
|
247
|
+
IsNever<Exclude<A, B>> extends true ? true : false,
|
|
248
|
+
IsNever<Exclude<B, A>> extends true ? true : false
|
|
249
|
+
>;
|
|
250
|
+
|
|
251
|
+
type EndIfEqual<I extends string, O extends string> =
|
|
252
|
+
AreStringsEqual<I, O> extends true
|
|
253
|
+
? never
|
|
254
|
+
: void;
|
|
255
|
+
|
|
256
|
+
function endIfEqual<I extends string, O extends string>(input: I, output: O): EndIfEqual<I, O> {
|
|
257
|
+
if (input === output) {
|
|
258
|
+
process.exit(0);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
endIfEqual('abc', 'abc');
|
|
263
|
+
//=> never
|
|
264
|
+
|
|
265
|
+
endIfEqual('abc', '123');
|
|
266
|
+
//=> void
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
@category Type Guard
|
|
270
|
+
@category Utilities
|
|
271
|
+
*/
|
|
272
|
+
type IsNever<T> = [T] extends [never] ? true : false;
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
An if-else-like type that resolves depending on whether the given type is `never`.
|
|
276
|
+
|
|
277
|
+
@see {@link IsNever}
|
|
278
|
+
|
|
279
|
+
@example
|
|
280
|
+
```
|
|
281
|
+
import type {IfNever} from 'type-fest';
|
|
282
|
+
|
|
283
|
+
type ShouldBeTrue = IfNever<never>;
|
|
284
|
+
//=> true
|
|
285
|
+
|
|
286
|
+
type ShouldBeBar = IfNever<'not never', 'foo', 'bar'>;
|
|
287
|
+
//=> 'bar'
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
@category Type Guard
|
|
291
|
+
@category Utilities
|
|
292
|
+
*/
|
|
293
|
+
type IfNever<T, TypeIfNever = true, TypeIfNotNever = false> = (
|
|
294
|
+
IsNever<T> extends true ? TypeIfNever : TypeIfNotNever
|
|
295
|
+
);
|
|
296
|
+
|
|
169
297
|
/**
|
|
170
298
|
Returns the static, fixed-length portion of the given array, excluding variable-length parts.
|
|
171
299
|
|
|
@@ -227,7 +355,7 @@ T extends readonly [...infer U] ?
|
|
|
227
355
|
/**
|
|
228
356
|
Returns whether the given array `T` is readonly.
|
|
229
357
|
*/
|
|
230
|
-
type IsArrayReadonly<T extends UnknownArray> = T extends unknown[] ? false : true
|
|
358
|
+
type IsArrayReadonly<T extends UnknownArray> = IfNever<T, false, T extends unknown[] ? false : true>;
|
|
231
359
|
|
|
232
360
|
type StringDigit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
|
|
233
361
|
|
|
@@ -324,49 +452,6 @@ type ShouldBeTrue = IsNegative<-1>;
|
|
|
324
452
|
*/
|
|
325
453
|
type IsNegative<T extends Numeric> = T extends Negative<T> ? true : false;
|
|
326
454
|
|
|
327
|
-
/**
|
|
328
|
-
Returns a boolean for whether the given type is `never`.
|
|
329
|
-
|
|
330
|
-
@link https://github.com/microsoft/TypeScript/issues/31751#issuecomment-498526919
|
|
331
|
-
@link https://stackoverflow.com/a/53984913/10292952
|
|
332
|
-
@link https://www.zhenghao.io/posts/ts-never
|
|
333
|
-
|
|
334
|
-
Useful in type utilities, such as checking if something does not occur.
|
|
335
|
-
|
|
336
|
-
@example
|
|
337
|
-
```
|
|
338
|
-
import type {IsNever, And} from 'type-fest';
|
|
339
|
-
|
|
340
|
-
// https://github.com/andnp/SimplyTyped/blob/master/src/types/strings.ts
|
|
341
|
-
type AreStringsEqual<A extends string, B extends string> =
|
|
342
|
-
And<
|
|
343
|
-
IsNever<Exclude<A, B>> extends true ? true : false,
|
|
344
|
-
IsNever<Exclude<B, A>> extends true ? true : false
|
|
345
|
-
>;
|
|
346
|
-
|
|
347
|
-
type EndIfEqual<I extends string, O extends string> =
|
|
348
|
-
AreStringsEqual<I, O> extends true
|
|
349
|
-
? never
|
|
350
|
-
: void;
|
|
351
|
-
|
|
352
|
-
function endIfEqual<I extends string, O extends string>(input: I, output: O): EndIfEqual<I, O> {
|
|
353
|
-
if (input === output) {
|
|
354
|
-
process.exit(0);
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
endIfEqual('abc', 'abc');
|
|
359
|
-
//=> never
|
|
360
|
-
|
|
361
|
-
endIfEqual('abc', '123');
|
|
362
|
-
//=> void
|
|
363
|
-
```
|
|
364
|
-
|
|
365
|
-
@category Type Guard
|
|
366
|
-
@category Utilities
|
|
367
|
-
*/
|
|
368
|
-
type IsNever<T> = [T] extends [never] ? true : false;
|
|
369
|
-
|
|
370
455
|
/**
|
|
371
456
|
Returns a boolean for whether two given types are both true.
|
|
372
457
|
|
|
@@ -537,9 +622,11 @@ If `<Fill>` is not provided, it will default to `unknown`.
|
|
|
537
622
|
|
|
538
623
|
@link https://itnext.io/implementing-arithmetic-within-typescripts-type-system-a1ef140a6f6f
|
|
539
624
|
*/
|
|
540
|
-
type BuildTuple<L extends number, Fill = unknown, T extends readonly unknown[] = []> =
|
|
541
|
-
?
|
|
542
|
-
:
|
|
625
|
+
type BuildTuple<L extends number, Fill = unknown, T extends readonly unknown[] = []> = number extends L
|
|
626
|
+
? Fill[]
|
|
627
|
+
: L extends T['length']
|
|
628
|
+
? T
|
|
629
|
+
: BuildTuple<L, Fill, [...T, Fill]>;
|
|
543
630
|
|
|
544
631
|
/**
|
|
545
632
|
Returns the maximum value from a tuple of integers.
|
|
@@ -1314,68 +1401,6 @@ type InternalPaths<T, Options extends Required<PathsOptions>> =
|
|
|
1314
1401
|
: never
|
|
1315
1402
|
: never;
|
|
1316
1403
|
|
|
1317
|
-
/**
|
|
1318
|
-
Convert a union type to an intersection type using [distributive conditional types](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
|
|
1319
|
-
|
|
1320
|
-
Inspired by [this Stack Overflow answer](https://stackoverflow.com/a/50375286/2172153).
|
|
1321
|
-
|
|
1322
|
-
@example
|
|
1323
|
-
```
|
|
1324
|
-
import type {UnionToIntersection} from 'type-fest';
|
|
1325
|
-
|
|
1326
|
-
type Union = {the(): void} | {great(arg: string): void} | {escape: boolean};
|
|
1327
|
-
|
|
1328
|
-
type Intersection = UnionToIntersection<Union>;
|
|
1329
|
-
//=> {the(): void; great(arg: string): void; escape: boolean};
|
|
1330
|
-
```
|
|
1331
|
-
|
|
1332
|
-
A more applicable example which could make its way into your library code follows.
|
|
1333
|
-
|
|
1334
|
-
@example
|
|
1335
|
-
```
|
|
1336
|
-
import type {UnionToIntersection} from 'type-fest';
|
|
1337
|
-
|
|
1338
|
-
class CommandOne {
|
|
1339
|
-
commands: {
|
|
1340
|
-
a1: () => undefined,
|
|
1341
|
-
b1: () => undefined,
|
|
1342
|
-
}
|
|
1343
|
-
}
|
|
1344
|
-
|
|
1345
|
-
class CommandTwo {
|
|
1346
|
-
commands: {
|
|
1347
|
-
a2: (argA: string) => undefined,
|
|
1348
|
-
b2: (argB: string) => undefined,
|
|
1349
|
-
}
|
|
1350
|
-
}
|
|
1351
|
-
|
|
1352
|
-
const union = [new CommandOne(), new CommandTwo()].map(instance => instance.commands);
|
|
1353
|
-
type Union = typeof union;
|
|
1354
|
-
//=> {a1(): void; b1(): void} | {a2(argA: string): void; b2(argB: string): void}
|
|
1355
|
-
|
|
1356
|
-
type Intersection = UnionToIntersection<Union>;
|
|
1357
|
-
//=> {a1(): void; b1(): void; a2(argA: string): void; b2(argB: string): void}
|
|
1358
|
-
```
|
|
1359
|
-
|
|
1360
|
-
@category Type
|
|
1361
|
-
*/
|
|
1362
|
-
type UnionToIntersection<Union> = (
|
|
1363
|
-
// `extends unknown` is always going to be the case and is used to convert the
|
|
1364
|
-
// `Union` into a [distributive conditional
|
|
1365
|
-
// type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
|
|
1366
|
-
Union extends unknown
|
|
1367
|
-
// The union type is used as the only argument to a function since the union
|
|
1368
|
-
// of function arguments is an intersection.
|
|
1369
|
-
? (distributedUnion: Union) => void
|
|
1370
|
-
// This won't happen.
|
|
1371
|
-
: never
|
|
1372
|
-
// Infer the `Intersection` type since TypeScript represents the positional
|
|
1373
|
-
// arguments of unions of functions as an intersection of the union.
|
|
1374
|
-
) extends ((mergedIntersection: infer Intersection) => void)
|
|
1375
|
-
// The `& Union` is to allow indexing by the resulting type
|
|
1376
|
-
? Intersection & Union
|
|
1377
|
-
: never;
|
|
1378
|
-
|
|
1379
1404
|
/**
|
|
1380
1405
|
Pick properties from a deeply-nested object.
|
|
1381
1406
|
|
|
@@ -2017,11 +2042,17 @@ array = split(items, ',');
|
|
|
2017
2042
|
type Split<
|
|
2018
2043
|
S extends string,
|
|
2019
2044
|
Delimiter extends string,
|
|
2045
|
+
> = SplitHelper<S, Delimiter>;
|
|
2046
|
+
|
|
2047
|
+
type SplitHelper<
|
|
2048
|
+
S extends string,
|
|
2049
|
+
Delimiter extends string,
|
|
2050
|
+
Accumulator extends string[] = [],
|
|
2020
2051
|
> = S extends `${infer Head}${Delimiter}${infer Tail}`
|
|
2021
|
-
?
|
|
2022
|
-
:
|
|
2023
|
-
?
|
|
2024
|
-
: [S];
|
|
2052
|
+
? SplitHelper<Tail, Delimiter, [...Accumulator, Head]>
|
|
2053
|
+
: Delimiter extends ''
|
|
2054
|
+
? Accumulator
|
|
2055
|
+
: [...Accumulator, S];
|
|
2025
2056
|
|
|
2026
2057
|
type GetOptions = {
|
|
2027
2058
|
/**
|
|
@@ -2224,7 +2255,7 @@ type Get<
|
|
|
2224
2255
|
BaseType,
|
|
2225
2256
|
Path extends
|
|
2226
2257
|
| readonly string[]
|
|
2227
|
-
| LiteralStringUnion<ToString<Paths<BaseType, {bracketNotation: false}> | Paths<BaseType, {bracketNotation: true}>>>,
|
|
2258
|
+
| LiteralStringUnion<ToString<Paths<BaseType, {bracketNotation: false; maxRecursionDepth: 2}> | Paths<BaseType, {bracketNotation: true; maxRecursionDepth: 2}>>>,
|
|
2228
2259
|
Options extends GetOptions = {}> =
|
|
2229
2260
|
GetWithPath<BaseType, Path extends string ? ToPath<Path> : Path, Options>;
|
|
2230
2261
|
|