タイプAliasとインタフェース


比較


共通点

  • オブジェクトタイプを定義および割り当てることができます.
  • classの実装.
  • 継承可能
  • // 인터페이스의 상속
    interface AInterface extends BInterface {}
       
    // type alias의 상속
    type AType = Btype & { a: number} 

    Only Interface

  • interfaceはインタフェース間のマージを許可
  • interface A {
      name: string;
    }
    
    interface A {
      age: number;
    }
    
    const obj: A = {
      // A interface가 다중으로 선언되었고, 이것으로 A는 merge 된다.
      // 따라서 A를 사용하기 위해서는 name과 age 모두 구현해야 한다.
    }

    Only Type Alias

  • Union type.
  • type Direction = 'left' | 'right' | 'up' | 'down';

    何を使うべきですか。


    Interface

  • 特定の仕様を定義し、その仕様によってオブジェクトまたはクラスを実装する必要がある場合は、interfaceを使用します.
  • interface「このインタフェースを実装するクラスはありますか?」こんな思いをさせられる.
  • ある目標の実施を宣言する.
  • Types (type alias)

  • データのタイプを特定する必要がある場合は、type aliasを使用します.