-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypes.ts
165 lines (124 loc) · 5.09 KB
/
types.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// JS Types (7) -------------------------------------------
const bool = true; // typeof bool -> 'boolean'
const num = 2; // typeof num -> 'number'
const str = 'str'; // typeof str -> 'string'
const nan = NaN; // typeof nan -> 'number'
const obj = {}; // typeof obj -> 'object'
const arr = []; // typeof arr -> 'object'
const nul = null; // typeof nul -> 'object'
const sym = Symbol(); // typeof sym -> 'symbol'
const und = undefined; // typeof und -> 'undefined'
const _void = void 0; // typeof _void -> 'undefined'
const fn = () => { }; // typeof fn -> 'function'
// TS Types (9+3) -----------------------------------------
type AllJsSimpleTypes = boolean | number | string | [] | object | undefined | null | void | symbol;
type StrangeTsTypes = any | unknown | never;
// Можно задавать переменную с определенным типом, или же ts определит тип автоматически как в js
let tsStr1: string = 'asd'; // equal
let tsStr2 = 'asd'; // equal
// Сложение всех чисел массива
function sumTS(arr: number[]) {
return arr.reduce((a, v) => a + v);
}
// Сложение числа и строки
const a = 2;
const b = '2';
const results = a + b;
if (typeof b === 'number') {
const result = a + b;
}
// Union type ---------------------------------------------
let strOrNum: string | number = '2';
strOrNum = 2;
// Alias type (переменная для типов) ----------------------
type StrOrNum = string | number;
const strOrNum1: StrOrNum = 1;
const strOrNum2: StrOrNum = '2';
const strOrNum3: StrOrNum = 3;
const strOrNum4: StrOrNum = '4';
// Array type ---------------------------------------------
const tsArr: number[] = [1, 2, 3];
const tsArrGeneric: Array<number> = [1, 2, 3];
const unionArr: (string | number)[] = [1, 2, '3'];
const unionArrGeneric: Array<string | number> = [1, 2, '3'];
// Tuple (массив фикс. длины) -----------------------------
const myTuple: [number, string] = [1, '2'];
// Деструкция таплов
const [val1, val2] = myTuple;
// Object type --------------------------------------------
const myObj = { a: 1, b: '2' };
const myObj1: { a: number, b: string } = { a: 1, b: '2' };
type MyObjType = { a: number, b: string }
const myObj2: MyObjType = { a: 1, b: '2' };
// Interface ----------------------------------------------
interface MyInterface {
readonly a: number; // нельзя переопределять
b: string;
c?: number[]; // optional type (необязательное)
}
const myObj3: MyInterface = {
a: 2,
b: '123',
}
// myObj3.a = 5; // readonly error
// myObj3.b = 5; // type error
myObj3.b = '5';
// для необязательных полей пишем проверку
if (myObj3.c) {
const val = myObj3.c;
}
const ApiAnswer: IndexInterface = { key: 'asd' }
const val3 = ApiAnswer.randomkey;
// индекс-сигнатура
interface IndexInterface {
[n: string]: string;
}
// Function -----------------------------------------------
function calculate(method: 'add' | 'sub', left: number, right: number): number {
switch (method) {
case 'add': return left + right;
case 'sub': return left - right;
}
}
const sum = calculate('add', 1, 2);
// Enum type ----------------------------------------------
// (способ ограничить количество методов для использования)
enum Methods {
add = '+',
sub = '-',
}
function calculateEnum(method: Methods, left: number, right: number): number {
switch (method) {
case Methods.add: return left + right;
case Methods.sub: return left - right;
}
}
const sumEnum = calculateEnum(Methods.add, 1, 2);
// Functions and alias ------------------------------------
const ArrowFn: TypeFn = () => 2;
type TypeFn = () => number;
interface FnInterface {
(a: number): void;
}
// Any type -----------------------------------------------
// (уник. переменная TS, выключает типизацию)
const someAny: any = 2;
someAny.reduce();
// Unknown type -------------------------------------------
// (уник. переменная TS, не является ни одним из типов)
const un: unknown = '2';
// un.concat(); // not working
if (typeof un === 'string') {
un.concat(); // working
}
// Void type ----------------------------------------------
// (тип функции, которая ничего не возвращает)
function voidFn(): void {
}
const someValue1 = voidFn();
// Never type ---------------------------------------------
// (тип функции, которая никогда не выполняется до конца)
function neverFn(): never {
throw new Error('my exception');
}
const someValue2 = neverFn();