πŸ›  TypeScript Essential - 2. Type Alias vs Interface

이좘길·2021λ…„ 10μ›” 28일
0
post-thumbnail

πŸ±β€πŸ λͺ©ν‘œ

  • Type Alias
  • Interface
  • Type Alias vs Interface

πŸ“Œ 1. Type Alias

1-1) κ°„λ‹¨ν•œ νƒ€μž… 지정

export type Name = string;
export type Email = string;
export type FooFunction = () => string; // (1)

export type TGetApi = { // (1)
  (url: string, search?: string): Promise<string>;
};

export type TUser = {
  readonly id: number; // (2)
  readonly name: string;
  email: Email;
  receiveInfo: boolean;
  active?: YesOrNo; // (3)
};
  • νƒ€μž… 별칭을 λ§Œλ“€μ–΄ μ•Œλ§žκ²Œ μ‚¬μš©ν•  수 μžˆλ‹€.
  • (1) ν•¨μˆ˜ νƒ€μž… 별칭은 두 가지 λ°©λ²•μœΌλ‘œ ꡬ성 κ°€λŠ₯
  • (2) 객체에 readonly 속성은 κ°’ λ³€κ²½ λΆˆκ°€λŠ₯
  • (3) Optional Property

1-2) λ¦¬ν„°λŸ΄ νƒ€μž… - Primitive type

export type YesOrNo = "Y" | "N";
export type DayOfWeek = "μ›”" | "ν™”" | "수" | "λͺ©" | "금" | "ν† " | "일";
export enum DayOfTheWeek {"μ›”", "ν™”", "수", "λͺ©", "금", "ν† ", "일"}
  • νƒ€μž…μ— 값을 μœ λ‹ˆμ˜¨ νƒ€μž… ν˜•νƒœλ‘œ 지정할 수 μžˆλ‹€.
  • 컴파일 μ‹œμ μ— λ³€μˆ˜κ°€ μ§€μ •λœ 값을 μ‚¬μš©ν•˜λŠ”μ§€ ν™•μΈν•œλ‹€.
  • Enum은 μ‹€μ œ 데이터에 집합, Type AliasλŠ” νƒ€μž… 체킹

1-3) λ¦¬ν„°λŸ΄ νƒ€μž… - Reference Type

export interface Color {
  fontColor: string;
  strokeColor: string;
  borderColor: string;
  backgroundColor: string;
}

export type Display = {
  display: "none" | "block";
  visibility: boolean;
  opacity: number;
};

export type Geometry = {
  width: number;
  height: number;
  padding: number;
  margin: number;
};

export type TStyle = Color &
  Display &
  Geometry & {
    tagName: string;
};
  • '&'λ₯Ό μ΄μš©ν•œ μΈν„°μ„Ήμ…˜ κΈ°λŠ₯을 톡해 Mergeλ₯Ό ν†΅ν•œ ν™•μž₯
  • Type Alias뿐만 μ•„λ‹ˆλΌ Interface둜 κ΅¬μ„±λœ νƒ€μž…λ„ ν™•μž₯ κ°€λŠ₯

1-4) 좔상적인 νƒ€μž…μ„ μ§€λ‹Œ 객체

export type TOnlyBooleanValueObject = {
  [prop: string]: boolean;
};
  • keyλŠ” string, 값은 boolean인 객체

1-5) 좔상적인 νƒ€μž…μ„ μ§€λ‹Œ ν•¨μˆ˜

export type TGetApiArrow = (url: string, search?: string) 
  => Promise<string>;

export type TGetApi = {
  (url: string, search?: string): Promise<string>;
};
  • url, searchλ₯Ό 인자둜 λ°›κ³  Promise 리턴을 ν•˜λŠ” ν•¨μˆ˜
  • 두 가지 ν˜•νƒœλ‘œ μ‚¬μš©λ  수 μžˆλ‹€.

πŸ“Œ 2. Interface

2-1) 객체에 λŒ€ν•œ νƒ€μž… 지정

export interface IUser {
  readonly id: number;
  readonly name: Name;
  email: string;
  receiveInfo: boolean;
  active: YesOrNo;
}
  • μΈν„°νŽ˜μ΄μŠ€λŠ” 객체에 λŒ€ν•œ νƒ€μž… 지정이 λͺ©μ 

2-2) μΈν„°νŽ˜μ΄μŠ€μ˜ 상속

// (1)
export interface IUser {
  address?: string;
}

export interface IUserProfile extends IUser {
  profileImage: string;
  github?: string;
  twitter?: string;
}

// (2)
export interface Color {
  fontColor: string;
  strokeColor: string;
  borderColor: string;
  backgroundColor: string;
}

export type Display = {
  display: "none" | "block";
  visibility: boolean;
  opacity: number;
};

export interface IStyle extends Color, Display {
  tagName: string;
}
  • (1) μΈν„°νŽ˜μ΄μŠ€λŠ” 상속을 톡해 νƒ€μž…μ„ κ²°ν•©ν•œλ‹€.
  • (2) Interface 이외에, Type Alias도 상속을 ν†΅ν•œ 결합이 κ°€λŠ₯

2-3) 좔상적인 νƒ€μž…μ„ μ§€λ‹Œ 객체

export interface IOnlyNumberValueObject {
  [key: string]: number;
}
  • keyλŠ” string, 값은 number인 객체

2-4) 좔상적인 νƒ€μž…μ„ μ§€λ‹Œ ν•¨μˆ˜

export interface IGetApi {
  (url: string, search?: string): Promise<string>;
}
  • url, searchλ₯Ό 인자둜 λ°›κ³  Promise 리턴을 ν•˜λŠ” ν•¨μˆ˜

2-5) μƒμ„±μž μΈν„°νŽ˜μ΄μŠ€

1) μƒμ„±μž μΈν„°νŽ˜μ΄μŠ€ 생성

export interface IRectConstruct {
  new (x: number, y: number, width: number, height: number): IRect;
}

2) μƒμ„±μž μΈν„°νŽ˜μ΄μŠ€ μ‚¬μš©

class Rect implements allTypes.IRect {
    id : number;
    x: number;
    y: number;
    width: number;
    height: number;

    constructor(x: number, y: number, width: number, height: number){
        this.id = Math.random() * 100000;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }
}

function createDefaultRect(cstor: allTypes.IRectConstruct){
    return new cstor(0, 0, 100, 100);
}

const rect1 = new Rect(0, 0, 100, 20);
const rect2 = createDefaultRect(Rect);

πŸ“Œ 3. Type Alias vs Interface

  • μ‘°μ§λ‚΄μ—μ„œ μˆ˜λ¦½ν•˜λŠ” 원칙이 μ€‘μš”ν•œ 포인트
  • μ˜ˆμ‹œ
    1) λ³€μˆ˜: νƒ€μž… λͺ…μ‹œ λͺ©μ , Type Alias μ‚¬μš©
    2) 객체: μΈν„°νŽ˜μ΄μŠ€ (λ©”μ†Œλ“œ, 클래슀 상속)

μ°Έκ³ 

패슀트캠퍼슀 κΉ€λ―Όνƒœμ˜ ν”„λ‘ νŠΈμ—”λ“œ 아카데미

profile
일지λ₯Ό κΎΈμ€€νžˆ μž‘μ„±ν•˜μž.

0개의 λŒ“κΈ€