
π κ°μ²΄μ λͺ¨μ(ꡬ쑰)μ μ μνλ μ€κ³λ
interface Person {
name: string;
age: number;
}
π μ΄λ κ² λ§λ€μ΄λλ©΄
const person: Person = {
name: "army",
age: 20
};
βοΈ κ°μ²΄λ λ°λμ μ΄ κ΅¬μ‘°λ₯Ό λ°λΌμΌ ν¨
| κ΅¬λΆ | type | interface |
|---|---|---|
| μ λμ¨ | β κ°λ₯ | β μ§μ λΆκ°λ₯ |
| μΈν°μΉμ | β κ°λ₯ | β μ§μ λΆκ°λ₯ |
| κ°μ²΄ μ μ | β κ°λ₯ | β κ°λ₯ |
| νμ₯ (μμ) | β κ°λ₯ | β κ°λ₯ |
| μ μΈ ν©μΉ¨ | β μλ¨ | β λ¨ |
type A = number | string; // μ λμ¨
type B = number & string; // μΈν°μΉμ
π interfaceλ μ΄λ κ² λͺ»ν¨ β
interface Person {
name: string;
age?: number; // μ νκ°
sayHi(): void;
sayHi(a: number, b: number): void;
}
π κ°μ ν¨μ μ¬λ¬κ° μ μ = μ€λ²λ‘λ©
const person: Person = {
name: "army",
sayHi: function () {
console.log("hello");
}
};
person.sayHi();
person.sayHi(1, 2);
age?: number;
π μμ΄λ λκ³ μμ΄λ λ¨
π λ€λ₯Έ μΈν°νμ΄μ€λ₯Ό μμλ°κΈ°
interface Animal {
name: string;
age: number;
}
interface Dog extends Animal {
isBark: boolean;
}
const dog: Dog = {
name: "",
age: 0,
isBark: true
};
interface DogCat extends Dog, Cat {}
π Dog + Cat λ λ€ ν©μΉ ꡬ쑰
const dogCat: DogCat = {
name: "",
age: 2,
isBark: true,
isScratch: true
};
π μΈν°νμ΄μ€λ κ°μ μ΄λ¦μΌλ‘ μ¬λ¬ λ² μ μΈ κ°λ₯
interface Person {
name: string;
}
interface Person {
age: number;
}
π μλμΌλ‘ ν©μ³μ§ π
const person: Person = {
name: "",
age: 20
};
type Person = { name: string }
type Person = { age: number } // β μλ¬
π λΌμ΄λΈλ¬λ¦¬ νμ λΆμ‘±ν λ μΆκ°
interface Lip {
a: number;
b: number;
}
interface Lip {
c: string;
}
π κ²°κ³Ό
const lip: Lip = {
a: 1,
b: 2,
c: "hello"
};
π μΈν°νμ΄μ€λ
π νμ (type)μ
β 1. extends νλλ° κ° μ λ£μ
interface Dog extends Animal
π Animal μμ± λ€ λ£μ΄μΌ ν¨!
β 2. optional μ°©κ°
age?: number;
π μμ΄λ λλκ±°μ§
π undefined μλ ν¬ν¨λ¨
β 3. ν¨μ μ€λ²λ‘λ© κ΅¬ν μν¨
sayHi(): void;
sayHi(a: number, b: number): void;
π μ€μ ꡬνμ νλλ§ μμ±ν΄μΌ ν¨
π interface = κ°μ²΄ ꡬ쑰 μ€κ³ + μμ + ν©μΉκΈ° κ°λ₯ν νμ