Generics(μ λ€λ¦)
ν¨μ, ν΄λμ€, μΈν°νμ΄μ€ λ± νμ μ λ§€κ°λ³μ μ²λΌ μ¬μ©ν μ μκ² νλ€.
λ€μν νμ μ λ°μ΄ν°λ₯Ό μ²λ¦¬ν μ μλ λ²μ©μ μΈ ν¨ν΄μ μ κ³΅ν΄ μ μ¬ν κΈ°λ₯μ νλ ν¨μ, ν΄λμ€ λ±μ νλλ‘ ν΅ν©ν μ μλ€.
- μ₯μ : μ½λ μ¬μ¬μ©μ± • μ μ°μ± • νμ₯μ± μ¦κ° (λΌμ΄λΈλ¬λ¦¬, ν΄ κ°λ° μ λμ± μ μ©) , λͺ μμ μΈ νμ νν
- λ¨μ : λ¬λ 컀λΈ(μ΄λ³΄μμκ² μ΄λ €μ) , 볡μ‘μ± • μ»΄νμΌ μκ° μ¦κ°
function genericFunction<T>(arg:T):T{
return arg;
}
interface GenericInterface<T> {}
class GenericClass<T> {}
νμ λ³μ(Type Variable)
νμ μ리μ λ체λ νμ μ λνλ΄λ λ³μ
μ€μ μ¬μ©μμλ ꡬ체μ μΈ νμ μΌλ‘ λ체λ¨
function ν¨μμ΄λ¦<Type>(arg:Type):Type{}
// Typeμ΄λ Tλ₯Ό μ£Όλ‘ μ¬μ©νμ§λ§ μ무거λ ν΄λ μκ΄μμ
λ€μκ³Ό κ°μ΄ μ¬μ©ν μ μλ€.
let numbers: Array<number> = [1, 2, 3, 4, 5]
let strings: Array<string> = ['1', '2', '3', '4', '5']
let div = document.querySelector<HTMLDivElement>('#myDiv1')
let btn = document.querySelector<HTMLButtonElement>('#myBtn1')
// μ λλ¦μΌλ‘ μ μν΄μ€¬κΈ° λλ¬Έμ λ²νΌμ μμ±μ μ κ·Όν μ μλ€
// btnμ μ€λ₯κ° λ¨ -> ν΄λΉ μμκ° μμ΄μ μλ¬κ° λλ κ²μ΄λ ?λ₯Ό λΆμ¬ μ‘°κ±΄λΆ νλ¨μ ν΄μ€
btn?.click()
μ λ€λ¦ ν¨μ
μ¬μ©νμ§ μμ κ²½μ°
function getFirstElement (arr:number[]) {
if(!arr.length) {
return undefined
}
return arr[0]
}
const firstNumber = getFirstElement(numbers)
function getFirstStringElement (arr:string[]) {
if(!arr.length) {
return undefined
}
return arr[0]
}
const firstString = getFirstStringElement(strings)
μ λ€λ¦μ μ¬μ©ν κ²½μ°
function getFirstElement<T>(arr: T[]): T | undefined {
if (!arr.length) {
return undefined
}
return arr[0]
}
const firstNumber = getFirstElement(numbers)
const firstString = getFirstElement(strings)
μ λ€λ¦ μΈν°νμ΄μ€
μ¬μ©νμ§ μμ κ²½μ°
interface strDict {
[key: string]: string
}
let strObj: strDict = {
name: "Elliot"
}
interface numDict {
[key: string]: number
}
let numObj: numDict = {
age: 23
}
μ λ€λ¦μ μ¬μ©ν κ²½μ°
interface Dict<T> {
[key: string]: T
}
let strObj: Dict<string> = {
name: "Elliot"
}
let numObj: Dict<number> = {
age: 23
}
μ¬λ¬ κ°μ νμ λ³μλ₯Ό μ§μ ν μλ μλ€.
μ΄λ°μμΌλ‘ λμ λ리, λ§΅ ꡬ쑰 μ¬μ©μ νμ μ κ΄ν΄ μ μ°νκ² μ²λ¦¬κ° κ°λ₯νλ€.
interface Entry<K, V> {
key: K;
value: V;
}
let entry1: Entry<string, number> = {
key: "age",
value: 25
}
let entry2: Entry<number, string[]> = {
key: 1,
value: ['red', 'green', 'blue']
}
μ λ€λ¦ ν΄λμ€
ν΄λμ€λ₯Ό μ μν λλ νμ μ κ³ μ νμ§ μκ³ ,
ν΄λμ€κ° μ¬μ©ν νμ μ μΈμ€ν΄μ€ν μμ μμ μ§μ ν μ μκ² ν΄μ€λ€.
μμ 1
class Item<T> {
#content: T | null;
constructor() {
this.#content = null;
}
setItem(value: T) {
this.#content = value;
}
getItem(): T | null {
return this.#content;
}
}
const numberItem = new Item<number>();
numberItem.setItem(100);
numberItem.getItem(); // 100
const stringItem = new Item<string>();
stringItem.setItem("Hello");
stringItem.getItem(); // Hello λ°ν
μμ 2-1
interface User {
id: number;
name: string;
}
interface Store<T> {
findById(id: number): T | undefined;
save(item: T): void;
}
class UserRepository implements Store<User> {
#users: User[] = []; // λΉ λ°°μ΄λ‘ μ΄κΈ°ν
findById(id: number): User | undefined {
return this.#users.find(user => user.id === id);
}
save(user: User): void {
this.#users.push(user)
}
}
// μ¬μ©
const userRepo = new UserRepository();
userRepo.save({
id: 1,
name: "Java"
})
userRepo.save({
id: 2,
name: "C#"
})
console.log(userRepo.findById(2))
μμ 2-2
μμμ μμ±ν Store μΈν°νμ΄μ€λ₯Ό μ¬μ©ν΄ μλ‘μ΄ ν΄λμ€λ₯Ό μμ±νλ€.
class ProductRepository implements Store<Product> {
#products: Product[] = [];
findById(id: number): Product | undefined {
return this.#products.find(product => product.id === id)
}
save(product: Product): void {
this.#products.push(product)
}
}
// μ¬μ©
const productRepo = new ProductRepository();
productRepo.save({
id: 10,
price: 100,
name: "keyboard"
})
productRepo.save({
id: 12,
price: 400,
name: "mouse"
})
console.log(productRepo.findById(12))
μ λ€λ¦μ μ μ½μ‘°κ±΄
μ μμ λ€μ κΈ°λ°μΌλ‘ μλ‘μ΄ μΈν°νμ΄μ€λ₯Ό μΆκ°νλ€.
interface User {
id: number; // id κ°μ μ μνμ§ μμΌλ©΄ Userλ₯Ό μ¬μ©νλ ν΄λμ€μμ id propertyλ₯Ό μ°Ύμ μ μλ€κ³ μλ¬κ° λλ€.
name: string;
}
// μλ‘ μΆκ°!!
interface WithId {
id: number;
}
// extends ν€μλλ‘ μ μ½ μ‘°κ±΄μ κ±Έμ΄μ€
interface Store<T extends WithId> {
findById(id: number): T | undefined;
save(item: T): void;
}
// Userμ idκ°μ΄ μλ€λ©΄ μλ¬κ° λ°μνκ² λ¨
class UserRepository implements Store<User> {
#users: User[] = []; // λΉ λ°°μ΄λ‘ μ΄κΈ°ν
findById(id: number): User | undefined {
return this.#users.find(user => user.id === id);
}
save(user: User): void {
this.#users.push(user)
}
}
μ΄λ° μμΌλ‘ μ μ½ μ‘°κ±΄μΌλ‘ λΆμ¬ν΄ νμ μ ꡬ체μ μΌλ‘ μ μν μ μλ€.
'typescript' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
TypeScript / Webpack (0) | 2025.06.10 |
---|---|
TypeScript / νμ μ μΈ νμΌ (4) | 2025.06.10 |
TypeScript / ν΄λμ€ (0) | 2025.06.05 |
TypeScript / νμ μΆλ‘ , νμ λ¨μΈ (0) | 2025.06.05 |
TypeScript / νμ (2) (0) | 2025.06.04 |