Notice
Recent Posts
Recent Comments
«   2026/02   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
Link
관리 메뉴

개발블로그

TypeScript - Type Alias 본문

TypeScript

TypeScript - Type Alias

춘식스 2022. 5. 27. 12:28
Type은 특정 타입에 별칭을 붙이는 용도로 사용하고 있다.
사용하여 객체를 위한 타입을 설정할 수도 있고, 배열 또는 그 어떤 타입이던 별칭을 지어줄 수 있다.
type Person1 = {
  name: string;
  age?: number;
};

// &는 Intersection 으로서 두개 이상의 타입들을 합쳐준다.
// 참고: https://www.typescriptlang.org/docs/handbook/advanced-types.html#intersection-types
type Developer1 = Person1 & {
  skills: string[];
};

const person2: Person1 = {
  name: '김사람'
};

const expert2: Developer1 = {
  name: '김개발',
  skills: ['javascript', 'react']
};

type People1 = Person1[]; // Person[] 를 이제 앞으로 People 이라는 타입으로 사용 할 수 있습니다.
const people1: People1 = [person2, expert2];

type Color = 'red' | 'orange' | 'yellow';
const color1: Color = 'red';
const colors: Color[] = ['red', 'orange']

주의할 점

  • 일관성있게 사용해야한다.
  • 라이브러리를 작성하거나 다른 라이브버리를 위한 타입 지원 파일을 작성하게 될 때는 interface를 사용하는 것을 권장한다.

'TypeScript' 카테고리의 다른 글

TypeScript - Class  (0) 2022.05.27
TypeScript - Generics  (0) 2022.05.27
TypeScript - Interface  (0) 2022.05.26
TypeScript 함수  (0) 2022.05.26
TypeScript 기본문법  (0) 2022.05.26
Comments