基本タイプ
オリジナルタイプ:数値、文字列、ブール値、シンボル 、ヌルまたは未定義の
文言リファレンス:let isDone: boolean = false;
配列
一つの方法:let list: number[] = [1, 2, 3];
方法:let list: Array<number> = [1, 2, 3];
元祖
タイプタプルは要素の数と既知のタイプのアレイ、必ずしも要素の同じタイプを表すことができます。
let x: [string, number];
x = ['hello', 10];
列挙
例えば、男性と女性、赤、緑、青のように列挙するために使用することができます
enum Color {Red, Green, Blue}
let c: Color = Color.Green;
どれか
let notSure: any = 4;
notSure = "maybe a string instead";
notSure = false;
空隙
関数が値を返さないときは、通常、その戻り値の型がvoidの表示されます。
function warnUser(): void {
console.log("This is my warning message");
}
決して
// 返回never的函数必须存在无法达到的终点
function error(message: string): never {
throw new Error(message);
}
オブジェクト
オブジェクトは、非プリミティブ型を表します
declare function create(o: object | null): void;
create({ prop: 0 }); // OK
create(null); // OK
create(42); // Error
型アサーション
「角カッコ」構文:
let someValue: any = "this is a string";
let strLength: number = (<string>someValue).length;
構文のように:
let someValue: any = "this is a string";
let strLength: number = (someValue as string).length;