TIL 42 | Type VS Interface

song hyun·2022년 1월 13일
0

TypeScript

목록 보기
2/4
post-thumbnail

✌ Type Alias

  • 타입 별칭(Typs Alias)는 타입의 새로운 이름을 생성한다.
  • 타입 별칭은 스스로를 참조 가능하다.
  • 타입 별칭은 선언적 확장이 불가능 하다.
// String Literal Type
type Easing = 'ease-in' | 'east-out' | 'ease-in-out';
let str:Easing = 'time'; // Error

// Object Literal Type
type person = {
  name:string;
  age:number;
};

type student = person & {
  school:string;
}; 

✌ Interface

  • 인터페이스는 일반적으로 타입 체크 위해 사용되며 변수, 함수, 클래스에 사용할 수 있다.
  • 인터페이스는 프로퍼티와 메서드를 가질 수 있다는 Class와 유사하다.
  • 하지만, 직접 인스턴스 생성할 수 없고, 모든 메서드는 추상 메서드이다.
interface Todo {
  id: number;
  content: string;
  completed: boolean;
}

let todo: Todo;

todo = { id: 1, content: 'typescript', completed: false };
profile
Front-end Developer 🌱

0개의 댓글