TypeScript (13)
2678 ワード
TypeScript (13)
1. Generics入力パラメータと戻りタイプ固定タイプ、同じ論理関数 は、より多くの反復関数 を生成する.はすべてのタイプの受信と戻り関数を作成し、Anyは予想と異なる結果 を得た.:文字列を挿入すると、文字列が表示されることを知っています.
文字列で使用可能な(.length)ですが、anyとは認識できません. だからgenericを使うのは です文字列の文字型 数字のデジタル機能は です. true不変量も である.
2. Generics Basic
1. Generics
function helloString(message: string): string {
return message;
}
function helloNumber(message: number): number {
return messagage;
}
function helloString(message: any): any {
return message;
}
問題点文字列で使用可能な(.length)ですが、anyとは認識できません.
function helloGeneric<T>(message: T) : T {
return message;
}
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 & Tuplefunction 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활용 가능
Reference
この問題について(TypeScript (13)), 我々は、より多くの情報をここで見つけました https://velog.io/@anthony16/TypeScript-13テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol