-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype-playground.ts
59 lines (51 loc) · 1.41 KB
/
type-playground.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
type Test<T = any> = {
config?: T;
[key: string]: any;
};
type dynamicType<T extends Test> = T['config'];
export type ArrayKeys = keyof any[];
export type Indices<T> = Exclude<keyof T, ArrayKeys>;
type ASD<T extends Test> = {
nested?: ASD<T['nested']>;
properties?: TupleToObject<UnionToTuple<keyof T['properties']>, T['properties']>;
config;
asd: dynamicType<T>;
};
type UnionToTuple<T> = (
(T extends any ? (t: T) => T : never) extends infer U
? (U extends any ? (u: U) => any : never) extends (v: infer V) => any
? V
: never
: never
) extends (_: any) => infer W
? [...UnionToTuple<Exclude<T, W>>, W]
: [];
type TupleToObject<Type extends readonly any[], R extends Record<any, any>> = {
[Key in Type[number]]: R[Key];
};
let x: TupleToObject<UnionToTuple<keyof { a: number; b: string }>, { a: number; b: string }> = { a: 23454534, b: 'string' };
function test<T extends ASD<T>>(obj: T) {}
test({
config: 90,
asd: 89090,
properties: {
name: {
config: '345',
asd: 'asd',
},
fghgf: {
config: 345,
asd: 7878,
},
},
});
type MyType = {
prop1: string;
prop2: number;
prop3: boolean;
};
type MyTypeKeysTuple<T> = {
[K in keyof T]: K;
};
let a: MyTypeKeysTuple<MyType>;
// MyTypeKeysTuple is now a tuple type with elements "prop1", "prop2", and "prop3"