Boolean

  1. let isDone: boolean = false;

Number

所有的 number 都是浮点数, 同时支持 hexadecimal 和 decimal 这两种 literal 的写法

  1. let decimal: number = 6;
  2. let hex: number = 0xf00d;

String

  1. let color: string = "blue";
  2. color = 'red';
  3. let fullName: string = `Bob Bobbington`;
  4. let age: number = 37;
  5. let sentence: string = `Hello, my name is ${ fullName }.

Array

  1. let list: number[] = [1, 2, 3]
  2. // or
  3. let list: Array<number> = [1, 2, 3]

Tuple

  1. 是一个元素数量固定的 array

  2. 元素的类型可以不一样

  1. let x: [string, number]; // delcare a tuple
  2. x = ['hello', 10]; // initialize it
  3. x = [10, 'hello']; // initialize it incorrectly
  4. // 超出下标范围的话, 使用 union type
  5. x[3] = "world"; // OK, 'string' can be assigned to 'string | number'
  6. console.log(x[5].toString()); // OK, 'string' and 'number' both have 'toString'
  7. x[6] = true; // Error, 'boolean' isn't 'string | number'

Enum

  1. enum Color {Red, Green, Bulue};
  2. // js: { '0': 'Red', '1': 'Green', '2': 'Blue', Red: 0, Green: 1, Blue: 2 }
  3. // 默认从 0 开始递增, 可以给初始值来改变这个行为
  4. enum Color {Red = 1, Green, Blue};
  5. // 当然也可以全部自由赋值
  6. enum Color {Red = 1, Green = 2, Blue = 4};
  7. // 看最上面的 js
  8. enum Color {Red = 1, Green, Blue};
  9. let colorName: string = Color[2];
  10. console.log(colorName); // Displays 'Green' as its value is 2 above

Any

any 来给不能确定类型的变量
Object 的区别:

  1. let notSure: any = 4;
  2. notSure.ifItExists(); // okay, ifItExists might exist at runtime
  3. notSure.toFixed(); // okay, toFixed exists (but the compiler doesn't check)
  4. let prettySure: Object = 4;
  5. prettySure.toFixed(); // Error: Property 'toFixed' doesn't exist on type 'Object'.

Void

就是 nullundefined , 给一个更统一的称呼而已

  1. function warnUser(): void {
  2. console.log("This is my warning message");
  3. }
  4. let unusable: void = undefined;
  5. Null and Undefined

Null and Undefined

  1. // Not much else we can assign to these variables!
  2. // 是的, null 和 undefined 都有自己的类型名字
  3. let u: undefined = undefined;
  4. let n: null = null;

默认来说, nullundefined 是其他类型的子类型, 因此可以赋值给其他类型的变量,
如果想改变这个默认行为的话, 那么可以使用 --strictNullChecks

Never

the type of never occur
never 是所有类型的子类, 它没有子类

  1. // Function returning never must have unreachable end point
  2. function error(message: string): never {
  3. throw new Error(message);
  4. }
  5. // Inferred return type is never
  6. function fail() {
  7. return error("Something failed");
  8. }
  9. // Function returning never must have unreachable end point
  10. function infiniteLoop(): never {
  11. while (true) {
  12. }
  13. }

Object

non-primitive type, 也就是除了 number, string, boolean, symbol, null, undefined 的其他类型

  1. declare function create(o: object | null): void;
  2. create({ prop: 0 }); // OK
  3. create(null); // OK
  4. create(42); // Error
  5. create("string"); // Error
  6. create(false); // Error
  7. create(undefined); // Error

Type assertions

ts 会自动推断变量的类型
如果自己明确地知道类型, 想要编译器闭嘴的时候, 可以使用 type assertions
有两种写法:

  1. // 尖括号
  2. let someValue: any = "this is a string";
  3. let strLength: number = (<string>someValue).length;
  4. // as, 写 JSX 的时候只能写这种
  5. let someValue: any = "this is a string";
  6. let strLength: number = (someValue as string).length;