Skip to content

Commit da9920c

Browse files
committed
add utility type and mapped type class code
1 parent f6ad888 commit da9920c

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

class-note/13_utility-type.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
interface Product {
2+
id: number;
3+
name: string;
4+
price: number;
5+
brand: string;
6+
stock: number;
7+
}
8+
9+
// 1. 상품 목록을 받아오기 위한 API 함수
10+
function fetchProducts(): Promise<Product[]> {
11+
// ..
12+
}
13+
14+
// interface ProductDetail {
15+
// id: number;
16+
// name: string;
17+
// price: number;
18+
// }
19+
20+
// 2. 특정 상품의 상세 정보를 나타내기 위한 함수
21+
type ShoppingItem = Pick<Product, 'id' | 'name' | 'price'>
22+
function displayProductDetail(shoppingItem: Pick<Product, 'id' | 'name' | 'price'>) {
23+
24+
}
25+
26+
// interface UpdateProduct {
27+
// id?: number;
28+
// name?: string;
29+
// price?: number;
30+
// brand?: string;
31+
// stock?: number;
32+
// }
33+
34+
type UpdateProduct = Partial<Product>
35+
// 3. 특정 상품 정보를 업데이트(갱신)하는 함수
36+
function updateProductItem(productItem: Partial<Product>) {
37+
38+
}
39+
40+
// 4. 유틸리티 타입 구현하기 - Partial
41+
interface UserProfile {
42+
username: string;
43+
email: string;
44+
profilePhotoUrl: string;
45+
}
46+
// interface UserProfileUpdate {
47+
// username?: string;
48+
// email?: string;
49+
// profilePhotoUrl?: string;
50+
// }
51+
// #1
52+
// type UserProfileUpdate = {
53+
// username?: UserProfile['username'];
54+
// email?: UserProfile['email'];
55+
// profilePhotoUrl?: UserProfile['profilePhotoUrl'];
56+
// }
57+
58+
// #2
59+
type UserProfileUpdate = {
60+
[p in 'username' | 'email' | 'profilePhotoUrl']?: UserProfile[p]
61+
}
62+
type UserProfileKeys = keyof UserProfile
63+
64+
// #3
65+
type UserProfileUpdate = {
66+
[p in keyof UserProfile]?: UserProfile[p]
67+
}
68+
69+
// #4
70+
type Subset<T> = {
71+
[p in keyof T]?: T[p]
72+
}

class-note/14_mapped-type.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
type Heroes = 'Hulk' | 'Capt' | 'Thor'
2+
type HeroAges = { [K in Heroes]: number }
3+
const ages: HeroAges = {
4+
Hulk: 33,
5+
Capt: 100,
6+
Thor: 1000,
7+
}
8+
9+
// for in 반복문 코드
10+
// var arr = ['a','b','c'];
11+
// for (var key in arr) {
12+
// console.log(arr[key]);
13+
// }

0 commit comments

Comments
 (0)