资料

中文文档

基础类型

  1. /* 布尔值 */
  2. let isDone: boolean = false;
  3. /*
  4. * 数字
  5. * TypeScript里的所有数字都是浮点数
  6. */
  7. let decLiteral: number = 6;
  8. /* 字符串 */
  9. let sentence: string = `Hello, my name is ${ isDone }.I'll be ${ decLiteral + 1 } years old next month.`;
  10. /* 数组 */
  11. let list: number[] = [1, 2, 3]; // 在元素类型后面接上 []
  12. let list: Array<number> = [1, 2, 3]; // 使用数组泛型,Array<元素类型>
  13. /*
  14. * 元组 Tuple
  15. * 元组类型允许表示一个已知元素数量和类型的数组,各元素的类型不必相同
  16. */
  17. let x: [string, number];
  18. // Initialize it
  19. x = ['hello', 10]; // OK
  20. // Initialize it incorrectly
  21. x = [10, 'hello']; // Error
  22. x = ['hello', 10,30,'hello']; // ok 越界的元素,会使用联合类型替代,值为 string|number 类型
  23. x = ['hello', 10, true]; // error
  24. /*
  25. * 枚举
  26. * 元组类型允许表示一个已知元素数量和类型的数组,各元素的类型不必相同
  27. */
  28. enum Color {Red, Green, Blue}
  29. let c: Color = Color.Green; // 1
  30. let i: string = Color[0]; // red
  31. // 默认情况下,从0开始为元素编号。 你也可以手动的指定成员的数值
  32. enum Color {Red = 1, Green, Blue}
  33. let c: Color = Color.Green; // 2
  34. /*
  35. * Any
  36. * 在编程阶段还不清楚类型的变量指定一个类型。 这些值可能来自于动态的内容,比如来自用户输入或第三方代码库。
  37. * 这种情况下,我们不希望类型检查器对这些值进行检查而是直接让它们通过编译阶段的检查。
  38. * 那么我们可以使用 any类型来标记这些变量:
  39. */
  40. let notSure: any = 4;
  41. notSure = "maybe a string instead";
  42. notSure = false; // okay, definitely a boolean
  43. // 在对现有代码进行改写的时候,any类型是十分有用的,它允许你在编译时可选择地包含或移除类型检查。 你可能认为 Object有相似的作用,就像它在其它语言中那样。 但是 Object类型的变量只是允许你给它赋任意值 - 但是却不能够在它上面调用任意的方法,即便它真的有这些方法:
  44. let notSure: any = 4;
  45. notSure.ifItExists(); // okay, ifItExists might exist at runtime
  46. notSure.toFixed(); // okay, toFixed exists (but the compiler doesn't check)
  47. let prettySure: Object = 4;
  48. prettySure.toFixed(); // Error: Property 'toFixed' doesn't exist on type 'Object'.
  49. // 当你只知道一部分数据的类型时,any类型也是有用的。 比如,你有一个数组,它包含了不同的类型的数据:
  50. let list: any[] = [1, true, "free"];
  51. list[1] = 100;
  52. /*
  53. * void :没有任何类型
  54. */
  55. function warnUser(): void {
  56. console.log("This is my warning message");
  57. }
  58. /*
  59. * Null 和 Undefined
  60. */
  61. /*
  62. * Never
  63. * 表示的是那些永不存在的值的类型,
  64. * never类型是那些总是会抛出异常
  65. * 或根本就不会有返回值的函数表达式或箭头函数表达式的返回值类型
  66. * 变量也可能是 never类型,当它们被永不为真的类型保护所约束时
  67. */
  68. // 返回never的函数必须存在无法达到的终点
  69. function error(message: string): never {
  70. throw new Error(message);
  71. }
  72. // 推断的返回值类型为never
  73. function fail() {
  74. return error("Something failed");
  75. }
  76. // 返回never的函数必须存在无法达到的终点
  77. function infiniteLoop(): never {
  78. while (true) {
  79. }
  80. }
  81. /* Object */
  82. declare function create(o: object | null): void;
  83. create({ prop: 0 }); // OK
  84. create(null); // OK
  85. create(42); // Error

类型断言

类型断言好比其它语言里的类型转换,有以下两种方式:

  1. // 尖括号
  2. let someValue: any = "this is a string";
  3. let strLength: number = (<string>someValue).length;
  4. // as 语法
  5. let someValue: any = "this is a string";
  6. let strLength: number = (someValue as string).length;


变量声明

接口

https://www.cnblogs.com/niklai/p/5778341.html
ts的核心原则之一是对值所具有的结构进行检查,接口的作用就是为这些类型命名和为你的代码或第三方代码定义契约

  1. interface LabelledValue {
  2. label: string;
  3. }
  4. function printLabel(labelledObj: LabelledValue) {
  5. console.log(labelledObj.label);
  6. }
  7. let myObj = {size: 10, label: "Size 10 Object"};
  8. printLabel(myObj);
  • 可选属性

只是在可选属性名字定义的后面加一个?符号

  1. interface SquareConfig {
  2. color?: string;
  3. width?: number;
  4. }
  5. function createSquare(config: SquareConfig): {color: string; area: number} {
  6. let newSquare = {color: "white", area: 100};
  7. if (config.color) newSquare.color = config.color;
  8. if (config.width) newSquare.area = config.width * config.width;
  9. return newSquare;
  10. }
  11. let mySquare = createSquare({color: "black"});
  • 只读属性

只读属性,不能再为他赋值

  1. interface Point {
  2. readonly x: number;
  3. readonly y: number;
  4. }
  • 额外的属性检查

  • 函数类型 ``` interface SearchFunc { (source: string, subString: string): boolean; }

let mySearch: SearchFunc; mySearch = function(source33: string, subString: string) { // 对于函数类型的类型检查来说,函数的参数名不需要与接口里定义的名字相匹配 let result = source.search(subString); return result > -1; }

  1. - 可索引的类型

interface StringArray {

} let myArray: StringArray; myArray = [“Bob”, “Fred”]; let myStr: string = myArray[0];

  1. TypeScript支持两种索引签名:字符串和数字。
  2. 可以同时使用两种类型的索引,但是**数字索引的返回值必须是字符串索引返回值类型的子类型**。
  3. 这是因为当使用
  4. `number`来索引时,JavaScript会将它转换成`string`然后再去索引对象。 也就是说用
  5. `100`(一个`number`)去索引等同于使用`"100"`(一个`string`)去索引,因此两者需要保持一致。

class Animal { name: string; } class Dog extends Animal { breed: string; } // 错误:使用数值型的字符串索引,有时会得到完全不同的Animal! interface NotOkay {

  1. [x: number]: Animal;
  2. [x: string]: Dog;

}

  1. **字符串索引签名能够很好的描述`dictionary`模式**,**并且它们也会确保所有属性与其返回值类型相匹配**。 因为字符串索引声明了
  2. `obj.property``obj["property"]`两种形式都可以。 下面的例子里,
  3. `name`的类型与字符串索引类型不匹配,所以类型检查器给出一个错误提示:

interface NumberDictionary {

length: number; // 可以,length是number类型 name: string // 错误,name的类型与索引类型返回值的类型不匹配 } ```

  • 类类型
  • 继承接口
  • 混合类型
  • 接口继承类

函数

泛型

枚举

类型理论

类型兼容性

高级类型

Symbols

迭代器和生成器

模块

命名空间

命名空间和模块

模块解析

声明合并

JSX

装饰器

Mixins

三斜线指令

javascript文件类型检查