-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path30970-shitariteraru.ts
101 lines (94 loc) · 4.23 KB
/
30970-shitariteraru.ts
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
92
93
94
95
96
97
98
99
100
101
/**
* 30970 - IsFixedStringLiteralType
*
* Sometimes you may want to determine whether a string literal is a definite type. For example, when you want to check whether the type specified as a class identifier is a fixed string literal type.
*
* ```typescript
* type Action<ID extends string> = { readonly id: ID };
* ```
*
* Since it must be fixed, the following types must be determined as false.
*
* * never type
* * Union of string literal types
* * Template literal types with embedded string, number, bigint, boolean
*
* Determine whether the given type S is a definite string literal type.
*
*
*/
/* _____________ Your Code Here _____________ */
type IsNever<T> = [T] extends [never] ? true : false;
type IsUnion<A, U = A> = IsNever<A> extends true
? false
: A extends unknown
? [U] extends [A]
? false
: true
: never;
type IsEmbedded<T> = string extends T
? true
: `${bigint}` extends T
? true
: `${number}` extends T
? true
: `${boolean}` extends T
? true
: `${(string & {})}` extends T
? true
: false;
type IsFixedStringLiteralType<S extends string> = IsNever<S> extends true
? false
: IsUnion<S> extends true
? false
: S extends `${infer A}${infer Rest extends string}`
? IsEmbedded<A> extends true
? false
: Rest extends ''
? true
: IsFixedStringLiteralType<Rest>
: false
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type testcase =
| Expect<Equal<IsFixedStringLiteralType<'ABC'>, true>>
| Expect<Equal<IsFixedStringLiteralType<string>, false>>
| Expect<Equal<IsFixedStringLiteralType<'ABC' | 'DEF'>, false>>
| Expect<Equal<IsFixedStringLiteralType<never>, false>>
| Expect<Equal<IsFixedStringLiteralType<`${string}`>, false>>
| Expect<Equal<IsFixedStringLiteralType<`${string & {}}`>, false>>
| Expect<Equal<IsFixedStringLiteralType<`${number}`>, false>>
| Expect<Equal<IsFixedStringLiteralType<`${bigint}`>, false>>
| Expect<Equal<IsFixedStringLiteralType<`${boolean}`>, false>>
| Expect<Equal<IsFixedStringLiteralType<`${true}`>, true>>
| Expect<Equal<IsFixedStringLiteralType<`${false}`>, true>>
| Expect<Equal<IsFixedStringLiteralType<`${null}`>, true>>
| Expect<Equal<IsFixedStringLiteralType<`${undefined}`>, true>>
| Expect<Equal<IsFixedStringLiteralType<`ABC${string}`>, false>>
| Expect<Equal<IsFixedStringLiteralType<`ABC${string & {}}`>, false>>
| Expect<Equal<IsFixedStringLiteralType<`ABC${number}`>, false>>
| Expect<Equal<IsFixedStringLiteralType<`ABC${bigint}`>, false>>
| Expect<Equal<IsFixedStringLiteralType<`ABC${boolean}`>, false>>
| Expect<Equal<IsFixedStringLiteralType<`ABC${true}`>, true>>
| Expect<Equal<IsFixedStringLiteralType<`ABC${false}`>, true>>
| Expect<Equal<IsFixedStringLiteralType<`ABC${null}`>, true>>
| Expect<Equal<IsFixedStringLiteralType<`ABC${undefined}`>, true>>
| Expect<Equal<IsFixedStringLiteralType<`${string}DEF`>, false>>
| Expect<Equal<IsFixedStringLiteralType<`${string & {}}DEF`>, false>>
| Expect<Equal<IsFixedStringLiteralType<`${number}DEF`>, false>>
| Expect<Equal<IsFixedStringLiteralType<`${bigint}DEF`>, false>>
| Expect<Equal<IsFixedStringLiteralType<`${boolean}DEF`>, false>>
| Expect<Equal<IsFixedStringLiteralType<`${true}DEF`>, true>>
| Expect<Equal<IsFixedStringLiteralType<`${false}DEF`>, true>>
| Expect<Equal<IsFixedStringLiteralType<`${null}DEF`>, true>>
| Expect<Equal<IsFixedStringLiteralType<`${undefined}DEF`>, true>>
| Expect<Equal<IsFixedStringLiteralType<`ABC${string}DEF`>, false>>
| Expect<Equal<IsFixedStringLiteralType<`ABC${string & {}}DEF`>, false>>
| Expect<Equal<IsFixedStringLiteralType<`ABC${number}DEF`>, false>>
| Expect<Equal<IsFixedStringLiteralType<`ABC${bigint}DEF`>, false>>
| Expect<Equal<IsFixedStringLiteralType<`ABC${boolean}DEF`>, false>>
| Expect<Equal<IsFixedStringLiteralType<`ABC${true}DEF`>, true>>
| Expect<Equal<IsFixedStringLiteralType<`ABC${false}DEF`>, true>>
| Expect<Equal<IsFixedStringLiteralType<`ABC${null}DEF`>, true>>
| Expect<Equal<IsFixedStringLiteralType<`ABC${undefined}DEF`>, true>>
| true