-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice.ts
More file actions
153 lines (118 loc) · 2.48 KB
/
practice.ts
File metadata and controls
153 lines (118 loc) · 2.48 KB
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
let count = 0;
count += 1;
const message: string = 'hello typescript';
const done: boolean = true
const numbers: number[] = [1, 2, 3];
const msg: string[] = ['hello', 'world'];
let mightBeUndefined: string | undefined = undefined;
let nullableNumber: number | null = null;
let color: 'red' | 'orange' | 'yellow' = 'red'
color = 'yellow'
function sum(x: number, y: number): number {
return x + y;
}
/**
*
* @param {number[]} numbers
*/
function sumArray(numbers: number[]): number {
return numbers.reduce((acc, current) => acc + current, 0);
}
function returnNothing(): void {
console.log('nothing')
}
const total = sumArray([1, 2, 3, 4]);
returnNothing();
interface Shape {
getArea(): number
}
class Circle implements Shape {
radius: number;
constructor(radius: number) {
this.radius = radius
}
getArea() {
return this.radius * this.radius * Math.PI;
}
}
class Rectangle implements Shape {
constructor(public width: number, public height: number) {
this.width = width;
this.height = height;
}
getArea() {
return this.width * this.height;
}
}
const shapes: Shape[] = [new Circle(5), new Rectangle(10, 5)];
shapes.forEach(shape => {
console.log(shape.getArea());
})
interface Person {
name: string
age?: number;
}
interface Developer extends Person {
skills: string[]
}
const man: Person = {
name: '홍길동',
age: 20
}
const expert: Developer = {
name: '강동기',
skills: ['javascript', 'react']
}
const people: Person[] = [man, expert]
function merge(a: any, b: any): any {
return {
...a,
...b
}
}
const merged = merge({ foo: 1 }, { bar: 1 });
console.log(merged);
function mergee<A, B>(a: A, b: B): A | B {
return {
...a,
...b
}
}
const mergeed = mergee(man, expert);
console.log(mergeed)
function warp<T>(param: T): T {
return param
}
const wrapped = warp([10]);
console.log(wrapped)
interface Items<T> {
list: Array<T>;
}
const items: Items<number> = {
list: [1, 2, 3]
}
console.log(items);
class Queue<T> {
list: T[] = [];
getlength(): number {
return this.list.length
}
enqueue(item: T): void {
this.list.push(item)
}
dequeue(): T | undefined {
return this.list.shift();
}
}
const queue = new Queue<number>();
console.log(queue);
queue.enqueue(0);
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
queue.enqueue(4);
console.log(queue.dequeue());
console.log(queue.dequeue());
console.log(queue.dequeue());
console.log(queue.dequeue());
console.log(queue.dequeue());