-
-
Notifications
You must be signed in to change notification settings - Fork 722
Expand file tree
/
Copy pathis-literal.d.ts
More file actions
91 lines (70 loc) · 2.02 KB
/
Copy pathis-literal.d.ts
File metadata and controls
91 lines (70 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import type {IsStringLiteral} from './is-string-literal.d.ts';
import type {IsNumericLiteral} from './is-numeric-literal.d.ts';
import type {IsBooleanLiteral} from './is-boolean-literal.d.ts';
import type {IsSymbolLiteral} from './is-symbol-literal.d.ts';
import type {IsNever} from './is-never.d.ts';
import type {IfNotAnyOrNever} from './internal/type.d.ts';
import type {CollapseLiterals, UnwrapBrand} from './internal/object.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).
@example
```
import type {IsLiteral} from 'type-fest';
type A = IsLiteral<1>;
//=> true
type B = IsLiteral<number>;
//=> false
type C = IsLiteral<1n>;
//=> true
type D = IsLiteral<bigint>;
//=> false
type E = IsLiteral<'type-fest'>;
//=> true
type F = IsLiteral<string>;
//=> false
type G = IsLiteral<`on${string}`>;
//=> false
declare const symbolLiteral: unique symbol;
type H = IsLiteral<typeof symbolLiteral>;
//=> true
type I = IsLiteral<symbol>;
//=> false
type J = IsLiteral<true>;
//=> true
type K = IsLiteral<false>;
//=> true
type L = IsLiteral<boolean>;
//=> false
type M = IsLiteral<1 | 'foo' | false>;
//=> true
type N = IsLiteral<string | number | symbol>;
//=> false
type O = IsLiteral<1000n | string | true>;
//=> boolean
```
@category Type Guard
@category Utilities
*/
export type IsLiteral<T> = IfNotAnyOrNever<T, {
ifNot: _IsLiteral<CollapseLiterals<UnwrapBrand<T>>>;
ifAny: false;
ifNever: false;
}>;
type _IsLiteral<T> =
| (Extract<T, boolean> extends infer Bools
// We can't instantiate `IsBooleanLiteral` with `never`,
// because that will add an extraneous `false` to the result if there are no booleans.
? IsNever<Bools> extends true
? never
: IsBooleanLiteral<Bools>
: never)
| (IsLiteralNonBools<Exclude<T, boolean>>);
type IsLiteralNonBools<T> =
T extends number | bigint
? IsNumericLiteral<T>
: T extends string
? IsStringLiteral<T>
: T extends symbol
? IsSymbolLiteral<T>
: false;
export {};