typescript04

타입 별칭(Type Alias)

  • Interface랑 비슷해 보인다.
  • Primitive, Union Type, Tuple, Function, 기타 직접 작성해야하는 타입을 다른 이름을 지정할 수 있습니다.
  • 만들어진 타입의 refer로 사용하는 것이지 타입을 만드는것은 아니다.

Aliasing Primitive

type MyStringType = string;

const str = 'world';

let myStr: MyStringType = 'hello';
myStr = str;

// 별 의미가 없다.

Aliasing Union Type

let person: string | number = 0;
person = 'Kdong';

type StringOrNumber = string | number;

let another: StringOrNumber = 0;
another = 'Omna';

// 1. 유니온 타입은 A도 가능하고, B도 가능한 타입
// 2. 길게 쓰는걸 짧게

Aliasing Tuple

let person: [string, number] = ['Kdong', 30];

type PersonTuple = [string, number];

let another: PersonTuple = ['Omna', 25];

// 1. 튜플 타입에 별칭을 줘서 여러군데서 사용할 수 있게 한다.

Aliasing Function

type EatType = (food: string) => void;

Aiasing vs interface

타입이 그 타입으로써 목적이 명확하면 interface

대상을 가리키거나, 별명으로써 존재하면 alias

 

Notion : https://torpid-pasta-de7.notion.site/Type-System-d3622ddf91d647679417c9cbc2db947c