InflearnTS Chapter 7~9 - YDP-SPLOUNGE-CLUB/typescript-study GitHub Wiki
νμ μμΌλ¦¬μΈμ€
μΈν°νμ΄μ€μ νμ κ³Όμ μ°¨μ΄κ° μμ§λ§ μ’ λ κ°μ²΄μ§ν₯μ μΌλ‘ νλ‘κ·Έλλ°μ ν λμλ μΈν°νμ΄μ€λ₯Ό μ¬μ©νλ νΈμ΄λ€.
νμ κ³Ό μΈν°νμ΄μ€κ° μλ²½νκ² λΆλ¦¬λμ΄ μλ κ²μ΄ μλ ννμ λ°©μμ μ°¨μ΄λΌκ³ μκ°νλ©΄ μ’λ€.
type Animal = { breath: true };
type Mammalia = Animal & { breed: true };
type Human = Mammalia & { think: true };
// μμ κΈ°λ₯
const me: Human = { breath: true, think: true, breed: true }
interface A {
breath: true
}
interface B extends A {
breed: true
}
// μΈν°νμ΄μ€μ type μ μμν μλ μλ€.
interface C extends Human {
}
νμ§λ§ μΈν°νμ΄μ€λ κ°μ μ΄λ¦μΌλ‘ μ μΈμ΄ κ°λ₯νλ€. μ¬λ¬λ² μ μΈν λλ§λ€ ν©μ³μ§λ€.
interface, enum, type κ°μ κ²½μ° λ³μμ I,E,Tλ₯Ό 맨 μμ λΆμ΄μ§λ§ μμ¦μλ μλΆμ΄λ μΆμΈλΌκ³ νλ€?
νμ μ μ§ν©μΌλ‘ μκ°νμ.
μ’μ νμ μμ λμ νμ μΌλ‘ λμ μ΄ κ°λ₯νλ€. λμ νμ μμ μ’μ νμ μΌλ‘ λμ μ΄ λΆκ°λ₯νλ€.
// λμ νμ
type A = string | number;
// μ’μ νμ
type B = string;
κ°μ²΄λ μμΈν μλ‘ μ’μ νμ μ΄λ€.
// λμ νμ
type A = { name: string };
type B = { age: number };
// μ’μ νμ
// type C = { name: string, age: number };
type AB = A | B;
type C = A & B;
// Cλ μ’μ νμ
μ΄λ©° AB λ λμ νμ
μ΄λ€. κ·Έλ¬λ―λ‘ cλ abλ₯Ό λμ
ν μ μλ€.
const ab1: AB = { name: 'name' };
const c1: C = ab1; // Error
// ABλ λμ νμ
μ΄λ©° Cλ μ’μ νμ
μ΄λ€.
const c2: C = { name: 'name', age: 1 };
const ab2: AB = c2; // OK
const ab3: AB = { name: 'name' };
// C λΌλ μ’μ νμ
μ married λΌλ λμ νμ
μ λμ
νμ§λ§ μλ¬κ° λμ¨λ€.
// κ°μ²΄μ 리ν°λ΄μ κ²μ¬ν λ μμ¬ μμ± κ²μ¬λΌλ μΆκ° κ²μ¬λ₯Ό νλ€.
// λ³μλ‘ λ°λ‘ λΉΌμ μ μλ₯Ό ν΄μ€λ€λ©΄ μμ¬ μμ± κ²μ¬λ₯Ό μ€ννμ§ μλλ€.
const c3: C = { name: 'name', age: 1, married: false }; // married False is not defined
void μ μ¬μ©λ²
return κ°μ΄ μλ κ²μ λ»νλ€.
// νμ§λ§ undefined λ λ°ν κ°λ₯νλ€.
function a(): void {
return undefined;
}
리ν΄κ°μ΄ void μΌ κ²½μ°μ λ§€κ°λ³μκ° void μΈ κ²½μ°, λ©μλλ‘ μ μΈν void, μΈ κ°μ§λ‘ λλ μ μλ€.
ν¨μμ μ§μ μ μΈ λ¦¬ν΄κ°μ΄ void μ κ²½μ°μλ§ return κ°μ μ무κ²λ 보λ΄μ μλλ©°. λ©μλμ void λ 리ν΄κ°μ μ¬μ©νμ§ μκ² λ€λ μλ―Έ
function a(callback: () => void): void {
}
a(() => {
return '3';
});
interface Human {
talk: () => void;
};
const human: Human = {
talk() { return '123' }
};
const aa = human.talk() as number; // Error
const bb = human.talk() as unknown as number // Error
// ν¨μμ νμ
μ μ μΈνκ³ κ΅¬νλλ₯Ό λ§λ€μ§ μμΌλ©΄ μλ¬κ° λ°μνλ€.
// Function implementation is missing or not immediately following the declaration
// function forEach(arr: number[], callback: (el: number) => undefined): void;
// declare λ₯Ό μ μΈνλ©΄ ν΄λΉ μ€λ₯λ μ¬λΌμ§λ€. λ€λ§ μλ°μ€ν¬λ¦½νΈ λ³νμ μ½λκ° μμ λλ€.
// undefined , void μ μ°¨μ΄ λ€μνλ² λ§νλ©΄ λ©μλ voidλ λ¦¬ν΄ κ°μ μ¬μ©νμ§ μκ² λ€λ λ».
declare function forEach2(arr: number[], callback: (el: number) => undefined): void;
let target: number[] = [];
forEach2([1,2,3], el => target.push(el)); // Error push μ return κ°μ number