TypeScript (13)


TypeScript (13)
1. Generics
function helloString(message: string): string {
  return message;
}
function helloNumber(message: number): number {
  return messagage;
}
  • 入力パラメータと戻りタイプ固定タイプ、同じ論理関数
  • は、より多くの反復関数
  • を生成する.
  • はすべてのタイプの受信と戻り関数を作成し、Anyは予想と異なる結果
  • を得た.
    function helloString(message: any): any {
      return message;
    }
    問題点
  • :文字列を挿入すると、文字列が表示されることを知っています.
    文字列で使用可能な(.length)ですが、anyとは認識できません.
  • だからgenericを使うのは
  • です
    function helloGeneric<T>(message: T) : T {
      return message;
    }
  • 文字列の文字型
  • 数字のデジタル機能は
  • です.
  • true不変量も
  • である.
    2. Generics Basic
    function helloBasic<T, U, K> 가능
    function helloBasic<T>(message: T) : T {
      return message;
    }
    呼び出し方法
    helloBasic<string>("Mark);  // 직접 타입을 지정
    helloBasic(36);             // T추론
    3. Generics Array & Tuple
    function helloArray<T>(message: T[]): T {
      return message[0];
    }
    helloArray(["Hello", "World"]);
    helloArray(["Hello", 5]);       // string와 number의 union type
                                    // .찍으면 string, number 모두 가능한 함수 
    function helloTuple<T, K>(message: [T,K]): T {
      return message[0];
    }
    helloTuple(["Hello", "World"]);
    helloTuple(["Hello", 5]);         // 정해진 length만큼의 배열이면 
                                     //  Generic 분리해서 type활용 가능