type vs interface

Younghwan Cha·2022년 10월 13일
0

typeScript

목록 보기
2/3

기본적으로 정의는 대문자로 시작한다.

interface Person {
	name: string;
    age: number;
}

type Person = {
	name: string;
    age: number;
}

확장 방법

interface Person {
	name: string;
    age: number;
}

interface Student extends Person {
	school: string;
}

type Person = {
	name: string;
    age: number;
}

type Student = Person & {
	school: string;
}

아래와 같이 Pick 을 통해서 필요한 타입만 사용 할 수 있다.

interface Todo {
  title: string;
  description: string;
  completed: boolean;
}

type TodoPreview = Pick<Todo, "title" | "completed">;
 
const todo: TodoPreview = {
  title: "Clean room",
  completed: false,
};

[ref]
https://github.com/microsoft/TypeScript/wiki/Performance#preferring-interfaces-over-intersections
https://www.typescriptlang.org/docs/handbook/utility-types.html

profile
개발 기록

0개의 댓글