-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.ts
99 lines (81 loc) · 2.17 KB
/
demo.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
type User = 'Marco' | 'Susi' | 'Tom'
let user: User
user = 'Marco'
user = 'Susi'
type AgeRating = 0 | 6 | 12 | 16 | 18
type ChildAgeRating = Exclude<AgeRating, 16 | 18>
type Genre = 'Action' | 'Horror' | 'Abenteuer' | 'Comedy'
const icons: {
[k in Genre as k extends 'Horror' ? never : Uncapitalize<k>]: string
} = {
action: 'svg',
abenteuer: 'svg',
comedy: 'svg'
}
function drawIcon(genre: Genre) {
const icon = icons[genre]
}
let a: 12 extends string ? boolean : number
type AllCombinationsOf<T, S = T> = [T] extends [never]
? []
: T extends S
? [T, ...AllCombinationsOf<Exclude<S, T>>]
: never
type AsArray1<T> = T[]
type AsArray2<T> = [T] extends [any] ? T[] : T
type A = AsArray1<AgeRating>
type B = AsArray2<AgeRating>
function getAgeRatings(): AllCombinationsOf<AgeRating> {
return [0, 6, 18, 16, 12]
}
type Movie = {
name: string
ageRating: AgeRating
genre: Genre
id: `M-${AgeRating}-${number}`,
director: {
name: string
country: {
isoCode: string,
number: number
}
}
}
let movie: Movie ={
name: 'Jurassic park',
ageRating: 12,
genre: 'Abenteuer',
id: 'M-12-43343'
}
type LoaderState<T> = {
status: 'success',
values: T[]
} | {
status: 'error',
error: string
} | {
status: 'loading'
}
let ml: LoaderState<Movie> = {
status: 'error',
error: 'Someerror'
}
type FlattenArray<T> = T extends (infer R)[] ? FlattenArray<R> : T
type X = FlattenArray<number>
type Y = FlattenArray<number[]>
type Z = FlattenArray<number[][][]>
type Join<K, P> = K extends string | number
? P extends (string | number)
? `${ K }${ '' extends P ? '' : '.' }${ P }`
: never
: never
export type Leaves<T> = T extends object
? { [K in keyof T]-?: Join<K, Leaves<T[K]>> }[keyof T] | ''
: '';
export type TypeAtLeave<SOURCE, PATH> = PATH extends `${ infer PROP }.${ infer REST }`
? SOURCE extends any ? PROP extends keyof SOURCE ? TypeAtLeave<SOURCE[PROP], REST> : never : never
: SOURCE extends any ? PATH extends keyof SOURCE ? SOURCE[PATH] : never : never
function updateSingleNestedOption<T extends Leaves<Movie>>(propertyName: T, value: TypeAtLeave<Movie, T>) {
//
}
updateSingleNestedOption('director.country.number', 12)