ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ Generics, Any ์ ๋ค๋ฅธ ์
๋ณดํต์ ํจ์๋ ๋ค์ด์ค๋ ์ธ์์ ๋๊ฐ๋ ๋ฆฌํด์ ํ์ ์ด ์ผ์ ํ ๊ท์น์ ์ด๋ฃจ๋ฉฐ ๊ฐ์ ๋ก์ง์ด ๋ฐ๋ณต๋๋ค.
function helloString(message: string): string {
return message;
}
function helloNumber(message: number): number {
return message;
}
๋ฐ๋ณต์ ์ค์ด๊ธฐ ์ํด ๋ชจ๋ ํ์ ์ ์ธ์๋ก ๋ฐ๊ณ ๋ฆฌํดํ ์ ์๋ any๋ฅผ ์ฌ์ฉํ ์ ์์ง๋ง ํ์ ์ด ์๋ก ์ผ์นํ์ง ์์ ์ ์๋ค๋ ๋ฌธ์ ๊ฐ ์๋ค.
function hello(message: any): any {
return message;
}
console.log(hello('Mark').length); // any์ length๋ any๋ก ์ถ๋ก
console.log(hello(38).length); // ์๋ฌ๊ฐ ์๋ undefined ์ถ๋ ฅ
hello ์ ๋ฆฌํด์ด any ์ด๊ธฐ ๋๋ฌธ์ ํ์
ํฌํผ๊ฐ ์ ๋๋ก ๋์ง ์๋๋ค.
์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ์ธ์ ํ์
์ ๋ณ์๋ก ํ์ฉํด ๋ฆฌํด ํ์
๊ณผ ๊ฐ์ด ์ฐ๊ด์ํค๊ธฐ ์ํ ๊ฒ์ด generic์ด๋ค.
T๋ ํ์
์ ์๋ฏธ, ํด๋น ํจ์ ๋ด์์ T์ ํ์
์ ๊ธฐ์ตํ์ฌ Mark๋ฅผ ๋ฃ์ผ๋ฉด string์ผ๋ก ๊ธฐ์ตํ๋ค.
function helloGeneric<T>(message: T): T {
return message;
}
console.log(helloGeneric('Mark')); //Mark
console.log(helloGeneric('Mark').length); //4
console.log(helloGeneric(36).length); //์๋ฌ
๐ข Generics Basic
- ๋ฐฉ๋ฒ 1. Generic ํ์ ์ ์ง์ ์ง์
- ๋ฐฉ๋ฒ 2. Generic ํ์ ์ ์ง์ ํ์ง ์๊ณ T๋ฅผ ์ถ๋ก
function helloBasic<T>(message: T): T {
return message;
}
//๋ฐฉ๋ฒ1.
helloBasic<string>('Mark'); //<string>๋ฅผ ํด์ฃผ๋ฉด ()์์ string ๋ง ๋ค์ด๊ฐ์ผํจ
//๋ฐฉ๋ฒ2.
helloBasic(38);
helloBasic<number>('38'); //์๋ฌ
๋ณ์๋ฅผ ์ฌ๋ฌ๊ฐ ๋ฃ์ ์๋ ์๋ค.
function helloBasic<T, U>(message: T, comment: U): T {
return message;
}
//๋ฐฉ๋ฒ1.
helloBasic<string, number>('Mark',30); //<string>๋ฅผ ํด์ฃผ๋ฉด ()์์ string ๋ง ๋ค์ด๊ฐ์ผํจ
//๋ฐฉ๋ฒ2.
helloBasic(38, 39);
๐ Generics Array & Tuple
๊ฐ ์ํฉ์ ๋ง์ถฐ array์ tuple์ ๊ตฌ๋ถํ์ฌ ์ง์ ํด์ฃผ๋ ๊ฒ์ด ์ค์ํ๋ค.
//array
function helloArray<T>(messages: T[]): T {
return messages[0];
}
console.log(helloArray(['Hello', 'World'])); // Hello
console.log(helloArray(['Hello', 1])); // ํ์
์ด ๋ค๋ฅด๋ฏ๋ก ๋ฆฌํดํ์
์ด string์ด๋ number๊ฐ ๋ ์ ์๋ค.
//tuple
function helloTuple<T, K>(messages: [T, K]): T {
return messages[0];
}
console.log(helloTuple(['Hello', 'World'])); // Hello
console.log(helloTuple(['Hello', 1])); // [string, number]์ด์ด์ string์ด ์ ์ถ๋ ฅ๋จ.
๐ฏ Generics Function
๊ทธ๋์ ํจ์๋ฅผ ์ง์ ๋ง๋ค์ด generic์ ์ฌ์ฉํ๋๋ฐ ์ด๋ฒ์ ํจ์์ ํ์
๋ง ์ ์ธํด๋ณด์.
type alias ๋ฐฉ์๊ณผ interface ๋ฐฉ์์ด ์๋ค.
//type alias
type HelloFunctionGeneric = <T>(message: T) => T;
const helloFunction: HelloFunctionGeneric = <T>(message: T): T => {
return message;
};
console.log(helloFunction<string>('Hello').length); //5
//interface
interface HelloFunctionGeneric1 {
<T>(message: T): T;
}
const helloFunction1: HelloFunctionGeneric = <T>(message: T): T => {
return message;
};
console.log(helloFunction1<string>('Hello').length); //5
๐ Generics Class
class์์์ generic์ ์ฌ์ฉํด๋ณด์.
class Person<T> { //T์ ์ ํจ๋ฒ์๋ class ์ ์ฒด
private _name: T;
constructor(name: T) {
this._name = name;
}
}
new Person('Mark');
new Person<string>(38); //์๋ฌ
๋ณ์๋ฅผ ์ฌ๋ฌ๊ฐ ์ง์ ํด๋ณด์.
class Person7<T, K> {
private _name: T;
private _age: K;
constructor(name: T, age: K) {
this._name = name;
this._age = age;
}
}
new Person7('Mark', 38);
๐ซ Generics with extends
generic์ผ๋ก ์ฌ์ฉํ ๋๋ extends๊ฐ ์์์ ๊ฐ๋
๊ณผ๋ ์ฝ๊ฐ ๋ค๋ฅด๋ค.
extends๋ก ํ์
์ ์ ์ฝ์ ๊ฑธ์ด์ฃผ๋ ๊ฒ์ด๋ฏ๋ก ์ฌ์ฉ์์ ์ค๋ฅ๋ฅผ ์ค์ด๊ฒ ๋๋ค.
generic์ ์ฌ์ฉํ ๋๋ extends๋ ๊ฐ์ด ์ฌ์ฉํ๋ ค๊ณ ๋
ธ๋ ฅํ์.
class PersonExtends<T extends string | number> { //string๊ณผ number๋ง ๊ฐ๋ฅ
private _name: T;
constructor(name: T) {
this._name = name;
}
}
new PersonExtends('Mark');
new PersonExtends(38);
new Person6(true); // T ๊ฐ string ๋๋ number๋ฅผ ์์๋ฐ๊ธฐ ๋๋ฌธ์ boolean ์ X
๐ keyof & type lookup system
interface Person8 {
name: string;
age: number;
}
const person8: Person8 = {
name: 'Mark',
age: 28
};
function getProp(obj: Person8, key: "name" | "age"): string | number {
return obj[key];
}
function setProp(obj: Person8, key: "name" | "age", value: string | number): void {
obj[key] = value;
}
getProp์ setProp์์ name/age์ ๋ฃ์ผ๋ฉด string/number์ธ mark/28์ด ๋ฐ๋์ ๋์์ผ ํ๋๋ฐ ๊ฒฐ๊ณผ๋ string ํน์ number์ด๋ฏ๋ก ์ค๋ฅ๊ฐ ์๊ธธ ์ ์๋ค.
์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ๋จผ์ keyof ๋ฅผ ์ฌ์ฉํด๋ณด์. keyof ๊ฐ์ฒด = ๋ฌธ์์ด ํํ์ key ๋ฅผ ๋ปํ๋ค.
function getProp(obj: Person8, key: keyof Person8): Person8[keyof Person8] {
return obj[key];
}
function setProp(obj: Person8, key: keyof Person8, value: string | number): void {
obj[key] = value;
}
๊ทธ๋ฐ๋ฐ Person8[keyof Person8] = Person8["name" | "age"] = Person8["name"] | person8["age"] = string | number ์ด๋ค.
์์ง๋ string ํน์ number๋ผ ์ํ๋ ๊ฒฐ๊ณผ๊ฐ ์ ํํ ์ ๋์ฌ ์ ์์ผ๋ ์ด์ generic์ ์ฌ์ฉํด๋ณด์.
Person8์ T๋ก ๋ฐ๊พธ๊ณ K๋ name๊ณผ age๋ง ๋ค์ด๊ฐ ์ ์๋๋ก extends๋ก ์ ์ํด์ค๋ค.
function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
function setProperty<T, K extends keyof T>(obj: T, key: K, value: T[K]): void {
obj[key] = value;
}
setProperty(person8, 'name', 'Anna');
setProperty(person8, 'name', 27); //์๋ฌ
getProperty(person8, 'name');
getProperty(person8, 'fullname');//์๋ฌ
'TypeScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ Classes (0) | 2022.12.19 |
---|---|
๐ Interfaces (0) | 2022.12.16 |
๐ TypeScript Compiler (0) | 2022.12.15 |