Skip to content

Commit a50d96a

Browse files
committed
from generic to quiz 2 solution
1 parent 6d496ce commit a50d96a

File tree

8 files changed

+217
-14
lines changed

8 files changed

+217
-14
lines changed

class-note/8_generics.ts

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// function logText(text) {
2+
// console.log(text);
3+
// return text;
4+
// }
5+
// logText(10); // 숫자 10
6+
// logText('하이'); // 문자열 하이
7+
// logText(true); // 진위값 true
8+
9+
// function logText<T>(text: T): T {
10+
// console.log(text);
11+
// return text;
12+
// }
13+
// logText<string>('하이');
14+
15+
// function logText(text: string) {
16+
// console.log(text);
17+
// // text.split('').reverse().join('');
18+
// return text;
19+
// }
20+
21+
// function logNumber(num: number) {
22+
// console.log(num);
23+
// return num;
24+
// }
25+
26+
// function logText(text: string | number) {
27+
// console.log(text);
28+
// return text;
29+
// }
30+
31+
// const a = logText('a');
32+
// logText(10);
33+
// const num = logNumber(10);
34+
// logText(true);
35+
36+
function logText<T>(text: T): T {
37+
console.log(text);
38+
return text;
39+
}
40+
41+
// const str = logText<string>('abc');
42+
// str.split('');
43+
// const login = logText<boolean>(true);
44+
45+
// logText('a')
46+
// logText(10)
47+
48+
// 인터페이스에 제네릭을 선언하는 방법
49+
// interface Dropdown {
50+
// value: string;
51+
// selected: boolean;
52+
// }
53+
54+
// const obj: Dropdown = { value: 'abc', selected: false };
55+
56+
interface Dropdown<T> {
57+
value: T;
58+
selected: boolean;
59+
}
60+
const obj: Dropdown<number> = { value: 'abc', selected: false };
61+
62+
// 제네릭 타입 제한
63+
// function logTextLength<T>(text: T[]): T[] {
64+
// console.log(text.length);
65+
// text.forEach(function (text) {
66+
// console.log(text);
67+
// });
68+
// return text;
69+
// }
70+
// logTextLength<string>(['hi', 'abc']);
71+
72+
// 제네릭 타입 제한 2 - 정의된 타입 이용하기
73+
interface LengthType {
74+
length: number;
75+
}
76+
function logTextLength<T extends LengthType>(text: T): T {
77+
text.length;
78+
return text;
79+
}
80+
logTextLength(10);
81+
logTextLength({ leng: 10 });
82+
83+
// 제네릭 타입 제한 3 - keyof
84+
interface ShoppingItem {
85+
name: string;
86+
price: number;
87+
stock: number;
88+
}
89+
90+
function getShoppingItemOption<T extends keyof ShoppingItem>(itemOption: T): T {
91+
return itemOption;
92+
}
93+
// getShoppingItemOption(10);
94+
// getShoppingItemOption<string>('a');
95+
getShoppingItemOption('name');

example/8_generics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@ function getAllowedOptions<T extends keyof ShoppingItems>(option: T): T {
5151
}
5252
getAllowedOptions('nothing');
5353
// const a = getAllowedOptions('name');
54-
// a.toUpperCase(); // Name
54+
// a.toUpperCase(); // Name

example/dropdown-generic.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
<div>
10+
<h1>이메일 선택 드롭다운</h1>
11+
<select id="email-dropdown">
12+
<!-- <option value="naver.com" selected>naver.com</option>
13+
<option value="google.com">google.com</option>
14+
<option value="hanmail.net">hanmail.net</option> -->
15+
</select>
16+
</div>
17+
<div>
18+
<h1>상품 수량 선택 드롭다운</h1>
19+
<select id="product-dropdown">
20+
<!-- <option value="1" selected>1</option>
21+
<option value="2">2</option>
22+
<option value="3">3</option> -->
23+
</select>
24+
</div>
25+
</body>
26+
</html>

example/dropdown-generic.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
interface DropdownItem<T> {
2+
value: T;
3+
selected: boolean;
4+
}
5+
6+
// interface Email {
7+
// value: string;
8+
// selected: boolean;
9+
// }
10+
11+
const emails: DropdownItem<string>[] = [
12+
{ value: 'naver.com', selected: true },
13+
{ value: 'gmail.com', selected: false },
14+
{ value: 'hanmail.net', selected: false },
15+
];
16+
17+
// interface ProductNumber {
18+
// value: number;
19+
// selected: boolean;
20+
// }
21+
22+
// interface TrueFalse {
23+
// value: boolean;
24+
// selected: boolean;
25+
// }
26+
27+
const numberOfProducts: DropdownItem<number>[] = [
28+
{ value: 1, selected: true },
29+
{ value: 2, selected: false },
30+
{ value: 3, selected: false },
31+
];
32+
33+
function createDropdownItem(item: DropdownItem<string> | DropdownItem<number>) {
34+
const option = document.createElement('option');
35+
option.value = item.value.toString();
36+
option.innerText = item.value.toString();
37+
option.selected = item.selected;
38+
return option;
39+
}
40+
41+
// NOTE: 이메일 드롭 다운 아이템 추가
42+
emails.forEach(function (email) {
43+
const item = createDropdownItem(email);
44+
const selectTag = document.querySelector('#email-dropdown');
45+
selectTag.appendChild(item);
46+
});
47+
48+
numberOfProducts.forEach(function (product) {
49+
const item = createDropdownItem(product);
50+
});

quiz/2_address-book/.eslintrc.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ module.exports = {
2323
arrowParens: 'avoid',
2424
},
2525
],
26-
'@typescript-eslint/no-explicit-any': 'off',
27-
"@typescript-eslint/explicit-function-return-type": 'off',
26+
// '@typescript-eslint/no-explicit-any': 'off',
27+
// "@typescript-eslint/explicit-function-return-type": 'off',
2828
'prefer-const': 'off',
2929
},
3030
parserOptions: {

quiz/2_address-book/src/a.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// function fetchItems() {
2+
// let items = ['a', 'b', 'c'];
3+
// return items;
4+
// }
5+
// let result = fetchItems();
6+
// console.log(result);
7+
8+
function fetchItems(): Promise<string[]> {
9+
let items: string[] = ['a', 'b', 'c'];
10+
return new Promise(function (resolve) {
11+
resolve(items);
12+
});
13+
}
14+
fetchItems();

quiz/2_address-book/src/index.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,17 @@ interface Contact {
1010
phones: PhoneNumberDictionary;
1111
}
1212

13+
enum PhoneType {
14+
Home = 'home',
15+
Office = 'office',
16+
Studio = 'studio',
17+
}
18+
1319
// api
1420
// TODO: 아래 함수의 반환 타입을 지정해보세요.
15-
function fetchContacts() {
21+
function fetchContacts(): Promise<Contact[]> {
1622
// TODO: 아래 변수의 타입을 지정해보세요.
17-
const contacts = [
23+
const contacts: Contact[] = [
1824
{
1925
name: 'Tony',
2026
address: 'Malibu',
@@ -57,45 +63,55 @@ function fetchContacts() {
5763
// main
5864
class AddressBook {
5965
// TODO: 아래 변수의 타입을 지정해보세요.
60-
contacts = [];
66+
contacts: Contact[] = [];
6167

6268
constructor() {
6369
this.fetchData();
6470
}
6571

66-
fetchData() {
72+
fetchData(): void {
6773
fetchContacts().then(response => {
6874
this.contacts = response;
6975
});
7076
}
7177

7278
/* TODO: 아래 함수들의 파라미터 타입과 반환 타입을 지정해보세요 */
73-
findContactByName(name) {
79+
findContactByName(name: string): Contact[] {
7480
return this.contacts.filter(contact => contact.name === name);
7581
}
7682

77-
findContactByAddress(address) {
83+
findContactByAddress(address: string): Contact[] {
7884
return this.contacts.filter(contact => contact.address === address);
7985
}
8086

81-
findContactByPhone(phoneNumber, phoneType: string) {
87+
// home, office, studio
88+
findContactByPhone(phoneNumber: number, phoneType: PhoneType): Contact[] {
8289
return this.contacts.filter(
8390
contact => contact.phones[phoneType].num === phoneNumber
8491
);
8592
}
93+
// findContactByPhone('officce');
8694

87-
addContact(contact) {
95+
addContact(contact: Contact): void {
8896
this.contacts.push(contact);
8997
}
9098

91-
displayListByName() {
99+
displayListByName(): string[] {
92100
return this.contacts.map(contact => contact.name);
93101
}
94102

95-
displayListByAddress() {
103+
displayListByAddress(): string[] {
96104
return this.contacts.map(contact => contact.address);
97105
}
98106
/* ------------------------------------------------ */
99107
}
100108

109+
// let heroes = [
110+
// { name: 'Tony', age: 30 },
111+
// { name: 'Captain', age: 100 },
112+
// ];
113+
// heroes.map(function (hero) {
114+
// return hero.name;
115+
// }); // ['Tony', 'Captain']
116+
101117
new AddressBook();

quiz/2_address-book/tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"checkJs": true,
55
"target": "es5",
66
"lib": ["es2015", "dom", "dom.iterable"],
7-
"noImplicitAny": false
7+
"noImplicitAny": true,
8+
"strict": true,
9+
"strictFunctionTypes": true
810
},
911
"include": ["./src/**/*"]
1012
}

0 commit comments

Comments
 (0)