-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntax.type
141 lines (123 loc) · 2.39 KB
/
syntax.type
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* ----------------------------------------
* The Basics
*/
/**
* Import types from another file
*/
import { Temp } from "./to-import"
/**
* Keyword Types
*/
type keywordTypes = [string, number, boolean, never, any, bigint, object]
/**
* Literal Types
*/
type literals = [
/**
* string: use double quote
*/
"abc",
/**
* boolean
*/
true, false,
/**
* number
*/
0, 1, 2,
/**
* array
*/
string[][],
/**
* template string
*/
`name: ${string}`,
/**
* object: use "," to separate members
*/
{
readonly a?: 1,
b: "abc",
c: {
a: boolean
}
}
]
/**
* Function Types
*/
type f1 = type () => void
type f2 = type(a: number, b: string) => number
type f3 = type () => type(a: number, b: string) => void
/**
* ----------------------------------------
* Create Type From Type
*/
/**
* Union & Intersection Types
*/
type u1 = union[0, 1, 2]
type u2 = | [0, 1, 2]
/* add parenthesis for function in union */
type u3 = union [
(type () => 1),
(type () => "1")
]
type i1 = combine[{ a: 1 }, { b: 2 }]
type i2 = & [{ a: 1 }, { b: 2 }]
/**
* Indexed Access Types
*/
type Person = { age: number, name: string, alive: boolean }
type Age = Person["age"]
/**
* Keyof Type Operator
*/
type Point = { x: number, y: number }
type P = keyof Point
/**
* Conditional Types
*/
/* type typeofNumber1 = 1 extends string ? "string" : "number" */
type typeofNumber1 = ^{
if(1 extends string) {
return "string"
} else {
return "number"
}
}
/**
* Generic Types
*/
/* export type Foo<T> = T extends { a: infer U; b: infer U; } ? U : never */
export type function Foo = (T) => ^{
if(T extends { a: infer U, b: infer U }) {
return U
} else {
return never
}
}
/**
* Mapped Types
*/
type Keys = union ["Name", "Age"]
/* type mapped1 = { [K in Keys]: boolean } */
type mapped1 = ^{
for(K in Keys) {
return {
key: K,
value: boolean
}
}
}
/* type mapped2 = { [K in Keys as `get${K}`]: () => string } */
type mapped2 = ^{
for(K in Keys) {
return {
key: `get${K}`,
value: type() => string
}
}
}