Generics 2๏ธโฃ
Keyof Constraint
์ ๋ค๋ฆญ์ผ๋ก ๊ฐ์ฒด์ ๋ํ ์ ์ฝ ์กฐ๊ฑด์ ์ค ๋, ํค ๊ฐ์ ํ์๋ก ํ๋ค๋ฉด keyof
๋ฅผ ์ฌ์ฉํ ์ ์ฝ ์กฐ๊ฑด์ ์ฐ๋ฉด ์ค์๋ฅผ ๋ฐฉ์งํ ์ ์๋ค.
function extractAndConvert<T extends object, U extends keyof T>(
obj: T,
key: U
) {
return 'Value: ' + obj[key];
}
extractAndConvert({}, 'name'); // Error occurred
์ ๋ค๋ฆญ T
์ ์ ์ฝ ์กฐ๊ฑด์ผ๋ก object
๋ฅผ ์ค์ ํด์ฃผ์๊ณ ,
U
์ ์ ์ฝ ์กฐ๊ฑด์ผ๋ก T
์ key
๋ฅผ ์ค์ ํด์ฃผ์๋ค.
extractAndConvert
๋ก ๋น ๊ฐ์ฒด({}
)์ name
์ด๋ผ๋ ๋ฌธ์์ด์ ์ ๋ฌํ์ ๋,
๋น ๊ฐ์ฒด์๋ name
์ด๋ผ๋ ํค ๊ฐ์ด ์๊ธฐ ๋๋ฌธ์ ์ปดํ์ผ ์ค๋ฅ๊ฐ ๋๋ค!
์ ๋ค๋ฆญ์ผ๋ก U extends keyof T
๋ฅผ ์ค์ ํ๊ธฐ ๋๋ฌธ์ ๋ฐ์ํ๋ ์ค๋ฅ์ด๋ฉฐ,
ํจ์๋ก name
ํค๋ฅผ ๊ฐ์ง ๊ฐ์ฒด๋ฅผ ์ ๋ฌํ๋ฉด ์ค๋ฅ๋ ์ฌ๋ผ์ง๋ค ๐
Generic Classes
์ ๋ค๋ฆญ์ผ๋ก ํด๋์ค๋ฅผ ๋ง๋ค ๊ฒฝ์ฐ ์๋ก์ด ์ธ์คํด์ค๋ฅผ ๋ง๋ค ๋ ์ ์ฐํ๊ฒ ํ์ ์ ๋ณ๊ฒฝํ ์ ์๋ค.
class DataStorage<T> {
private data: T[] = [];
addItem(item: T) {
this.data.push(item);
}
removeItem(item: T) {
this.data.splice(this.data.indexOf(item), 1);
}
getItems() {
return [...this.data];
}
}
const textStorage = new DataStorage<string>();
textStorage.addItem('Apeach');
textStorage.addItem('Rabbit');
textStorage.removeItem('Apeach');
console.log(textStorage.getItems()); // ['Rabbit']
๋ง์ฝ ์ ์์ ์์ ์ ๋ค๋ฆญ์ ์ฌ์ฉํ์ง ์๊ณ class
์์ฒด์ ํ์
์ ์ง์ ํ๋ค๋ฉด string | number
๋ฑ๋ฑ...
ํ์ํ ํ์ ์ ์ง์ ์ถ๊ฐํด์ผํ๋ ๋ฒ๊ฑฐ๋ก์์ด ์์์ ๊ฒ์ด๋ค ๐ค
์ ์ธ ์์ ์ด ์๋๋ผ ์์ฑ ์์ ์ ์ํ๋ ํ์ ์ ์ง์ ํ ์ ์๋ค๋ ๊ฒ์ ์ ๋ค๋ฆญ์ ๋ง๊ฐํ ์ฅ์ !
๋ฌผ๋ก ์ ์ฝ ์กฐ๊ฑด์ ์ ๋ค๋ฆญ์ ์ถ๊ฐํ๋ ๋ฐฉ๋ฒ๋ ์๋ค ๐
Partial type
Partial ํ์ ์ ์ ๋ค๋ฆญ์ผ๋ก ์ ๋ฌ๋ ํ์ ์ด ๊ฐ์ฒด์์ ์๋ ค์ฃผ๋ ์ญํ ์ ํ๋ฉด์
์ ๋ฌ๋ ํ์ ์ ์์ฑ๋ ๋ชจ๋ ์์ฑ์ ์ ํ์ ํ์ ์ผ๋ก ๋ณ๊ฒฝํด์ค๋ค.
interface CourseGoal {
title: string;
description: string;
completeUntil: Date;
}
function createCourseGoal(
title: string,
description: string,
date: Date
): CourseGoal {
let courseGoal: Partial<CourseGoal> = {};
courseGoal.title = title;
courseGoal.description = description;
courseGoal.completeUntil = date;
return courseGoal as CourseGoal;
}
Readonly type
Readonly
ํ์
์ ์ ๋ค๋ฆญ์ผ๋ก ์ ๋ฌ๋ ํ์
์ด ๋ฌด์กฐ๊ฑด ์ฝ๊ธฐ๋ง ๊ฐ๋ฅํ๋ค๊ณ ์๋ ค์ฃผ๋ ์ญํ ์ ํ๋ค.
Readonly
ํ์
์ ์ฌ์ฉํ๋ฉด ์์ฑ ๋ณ๊ฒฝ ๋ฐ ์ถ๊ฐ๊ฐ ๋ถ๊ฐ๋ฅํ๋ค ๐ต
const names: Readonly<string[]> = ['Shou', 'Apeach'];
names.push('Rabbit'); // Error occurred
names.pop(); // Error occurred
Utility Types
https://www.typescriptlang.org/docs/handbook/utility-types.html
Generics vs Union types
class DataStorage<T> {
private data: T[] = [];
addItem(item: T) {
this.data.push(item);
}
removeItem(item: T) {
this.data.splice(this.data.indexOf(item), 1);
}
getItems() {
return [...this.data];
}
}
const numberStorage = new DataStorage<number>();
์ ๋ค๋ฆญ์ ๊ฒฝ์ฐ ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ ์์ ์ number
ํ์
์ ๋ฐฐ์ด์ ํ์
์ผ๋ก ์ง์ ํ๊ฒ ๋ค๊ณ ์ง์ ์ด ๊ฐ๋ฅํ์ง๋ง,
class DataStorage {
private data: (string | number | boolean)[] = [];
addItem(item: string | number | boolean) {
this.data.push(item);
}
removeItem(item: string | number | boolean) {
this.data.splice(this.data.indexOf(item), 1);
}
getItems() {
return [...this.data];
}
}
union
ํ์
์ data
๋ผ๋ ๋ฐฐ์ด์ any
ํ์
์ ์ง์ ํด์ค ๊ฒ๊ณผ ๋ค๋ฆ์๋..๐ซข (์์ ๊ฐ ์ค์ ๋ก any
์ฒ๋ผ ๋์ํ์ง๋ ์์ง๋ง)
์ด ๊ฒฝ์ฐ data
์ ํ์
์ string[] | number[] | boolean[]
์ผ๋ก ์ค์ ํด์ค ์ ์๊ฒ ์ผ๋
์ด๋ ๊ฒ ๋๋ฉด data
๋ฅผ ์ด๋ค ํ์
์ ๋ฐฐ์ด๋ก ์ค์ ํ๋๋์ ๋ฐ๋ผ item
์ ํ์
๋ํ ์ ๋์ ์ผ๋ก ๋ฌ๋ผ์ ธ์ผ ํ๊ธฐ ๋๋ฌธ์ item
์ ํ์
์ค์ ์ด ์ด๋ ค์์ง๋ค ๐ก
์ ๋ค๋ฆญ์ ํน์ ํ์ ์ ๊ณ ์ ํ๊ฑฐ๋, ์์ฑํ ์ ์ฒด ํด๋์ค ์ธ์คํด์ค์ ๊ฑธ์ณ ๊ฐ์ ํจ์๋ฅผ ์ฌ์ฉํ๊ฑฐ๋, ์ ์ฒด ํจ์์ ๊ฑธ์ณ ๊ฐ์ ํ์ ์ ์ฌ์ฉํ ๋ ์ ์ฉํ๋ค.union
ํ์ ์ ๋ชจ๋ ๋ฉ์๋ ํธ์ถ์ด๋ ๋ชจ๋ ํจ์ ํธ์ถ๋ง๋ค ๋ค๋ฅธ ํ์ ์ ์ง์ ํ๊ณ ์ ํ๋ ๊ฒฝ์ฐ์ ์ ์ฉํ๋ค.
'๐ Study > TypeScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[TS] Project Drag & Drop 1๏ธโฃ (2) | 2023.03.27 |
---|---|
[TS] Decorators 3๏ธโฃ (0) | 2023.03.24 |
[TS] Decorators 2๏ธโฃ (0) | 2023.03.22 |
[TS] Decorators 1๏ธโฃ (1) | 2023.03.20 |
[TS] Generics 1๏ธโฃ (4) | 2023.03.16 |