-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
110 lines (84 loc) · 1.78 KB
/
main.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
let id = 5;
id = 7;
let productIds: number[] = [];
// Tuple
let person: [number, string, boolean] = [1, 'Tim', true];
// Tuple array
let employee: [string, boolean][];
employee = [
['foo', true],
['bar', false],
['baz', false],
];
// Union
let offer: string | number | boolean = 2;
offer = '2';
offer = false;
// Objects
type User = {
id: number | string;
name: string;
};
const user: User = {
id: '500',
name: 'Foo Bar',
};
// Type assertion
let cid: any = 1;
let customerId = <number>cid;
customerId = 6;
// Functions
const sum = (x: number, y: number): number => {
return x + y;
};
const myMessage = (messasge: number | string): void => {
console.log(messasge);
};
myMessage('true');
// Interface
interface memberInterface {
id: number;
name: string;
paid: boolean;
agr?: string;
}
const memberData: memberInterface = {
id: 39138281,
name: 'Tim Tam',
paid: false,
};
// Classes
interface PlayerInterface {
jerseyNo: number;
jerseyName: string;
playsFor(): string;
}
class basketballPlayer implements PlayerInterface {
jerseyNo: number;
jerseyName: string;
constructor(jerseyNo: number, jerseyName: string) {
this.jerseyNo = jerseyNo;
this.jerseyName = jerseyName;
}
playsFor() {
return `${this.jerseyName} plays basketball`;
}
}
const sc30 = new basketballPlayer(30, 'Curry');
console.log(sc30.playsFor());
// Subclass
class Position extends basketballPlayer {
position: string;
constructor(id: number, name: string, position: string) {
super(id, name);
this.position = position;
}
}
const kb24 = new Position(24, 'Kobe', 'SG');
console.log(kb24.position);
// Generics
const getArray = <T>(items: T[]): T[] => {
return new Array().concat(items);
};
let numArray = getArray<number>([1, 2, 3, 4]);
numArray.push(1);