본문 바로가기
Language/TypeScript

타입과 인터페이스 상속

by Zayne 2023. 2. 11.

■ 타입 상속

type Animal = { breath: true };
type Cat = Animal & { breed: true };
type Human = Cat & { think: true };
// type 에서 & 를 상속의 개념으로 사용

const lee: Human = { breath: true, breed: true, think: true };

 

■ 인터페이스 상속

interface A {
  breath: true;
}
interface B extends A {
  breed: true;
}
// interface 에서는 extends 키워드를 사용하여 상속
const b: B = { breath: true, breed: true };

// type Human 을 extends 키워드를 이용하여 상속 가능
interface C extends Human {}

const c: C = { breath: true, breed: true, think: true };

 

■ 인터페이스 특징

// interface 특징 중 하나는 다음과 같이 더해갈 수 있음
interface D {
  talk: () => void;
}
interface D {
  eat: () => void;
}
interface D {
  sit: () => void;
}

const d: D = { talk() {}, eat() {}, sit() {} };

'Language > TypeScript' 카테고리의 다른 글

unknown, any  (0) 2023.02.15
void  (0) 2023.02.13
union(|)과 intersection(&)  (0) 2023.02.09
enum, keyof, typeof  (0) 2023.02.07
원시 래퍼 타입, 템플릿 리터럴 타입, rest, 튜플  (0) 2023.02.05