1.类型别名 type

  1. type Name = string;
  2. type NameResolver = () => string;
  3. type NameOrResolver = Name | NameResolver;
  4. function getName(n: NameOrResolver): Name {
  5. if (typeof n === 'string') {
  6. return n;
  7. } else {
  8. return n();
  9. }
  10. }

2.字符串字面量类型

  1. type EventNames = 'click' | 'scroll' | 'mousemove';

3.元组

数组合并了相同类型的对象,而元组(Tuple)合并了不同类型的对象。

  1. let tom: [string, number] = ['Tom', 25];
  2. //给元组赋值可只赋值其中一项,但是当直接对元组类型的变量进行初始化或者赋值的时候,需要提供所有元组类型中指定的项。
  3. //当添加越界的元素时,它的类型会被限制为元组中每个类型的联合类型。

4.枚举

  1. enum Days {Sun, Mon, Tue, Wed, Thu, Fri, Sat};
  2. console.log(Days["Sun"] === 0); // true
  3. console.log(Days["Mon"] === 1); // true
  4. console.log(Days["Tue"] === 2); // true
  5. console.log(Days["Sat"] === 6); // true
  6. console.log(Days[0] === "Sun"); // true
  7. console.log(Days[1] === "Mon"); // true
  8. console.log(Days[2] === "Tue"); // true
  9. //枚举编译后
  10. var Days;
  11. (function (Days) {
  12. Days[Days["Sun"] = 0] = "Sun";
  13. Days[Days["Mon"] = 1] = "Mon";
  14. Days[Days["Tue"] = 2] = "Tue";
  15. Days[Days["Wed"] = 3] = "Wed";
  16. Days[Days["Thu"] = 4] = "Thu";
  17. Days[Days["Fri"] = 5] = "Fri";
  18. Days[Days["Sat"] = 6] = "Sat";
  19. })(Days || (Days = {}));

1.手动赋值

  1. enum Days {Sun = 7, Mon = 1, Tue, Wed, Thu, Fri, Sat};
  2. console.log(Days["Sun"] === 7); // true
  3. console.log(Days["Mon"] === 1); // true
  4. console.log(Days["Tue"] === 2); // true
  5. console.log(Days["Sat"] === 6); // true
  6. //手动赋值的枚举项也可以为小数或负数,此时后续未手动赋值的项的递增步长仍为 1:
  7. enum Days {Sun = 7, Mon = 1.5, Tue, Wed, Thu, Fri, Sat};
  8. console.log(Days["Sun"] === 7); // true
  9. console.log(Days["Mon"] === 1.5); // true
  10. console.log(Days["Tue"] === 2.5); // true
  11. console.log(Days["Sat"] === 6.5); // true

5.类

1.public

修饰的属性或方法是公有的,可以在任何地方被访问到,默认所有的属性和方法都是

  1. class Animal {
  2. public name;
  3. public constructor(name) {
  4. this.name = name;
  5. }
  6. }
  7. let a = new Animal('Jack');
  8. console.log(a.name); // Jack
  9. a.name = 'Tom';
  10. console.log(a.name); // Tom

2.private


使用 private 修饰的属性或方法,在子类中也是不允许访问的

3.protected


如果是用 protected 修饰的属性或方法,则允许在子类中访问:

4.修饰构造函数

当构造函数修饰为 private 时,该类不允许被继承或者实例化;
当构造函数修饰为 protected 时,该类只允许被继承:

5.抽象类

abstract 用于定义抽象类和其中的抽象方法。

  1. 1. //首先,抽象类是不允许被实例化的:
  2. abstract class Animal {
  3. public name;
  4. public constructor(name) {
  5. this.name = name;
  6. }
  7. public abstract sayHi();
  8. }
  9. let a = new Animal('Jack');
  10. // index.ts(9,11): error TS2511: Cannot create an instance of the abstract class 'Animal'.
  1. 2. //其次,抽象类中的抽象方法必须被子类实现:
  2. abstract class Animal {
  3. public name;
  4. public constructor(name) {
  5. this.name = name;
  6. }
  7. public abstract sayHi(); //抽象方法
  8. }
  9. class Cat extends Animal {
  10. public eat() {
  11. console.log(`${this.name} is eating.`);
  12. }
  13. }
  14. let cat = new Cat('Tom');
  15. // index.ts(9,7): error TS2515: Non-abstract class 'Cat' does not implement inherited abstract member 'sayHi' from class 'Animal'.

6.类的类型

  1. class Animal {
  2. name: string;
  3. constructor(name: string) {
  4. this.name = name;
  5. }
  6. sayHi(): string {
  7. return `My name is ${this.name}`;
  8. }
  9. }
  10. let a: Animal = new Animal('Jack');
  11. console.log(a.sayHi()); // My name is Jack

https://ts.xcatliu.com/advanced/class

6.类与接口

1.implements 类实现接口

实现(implements)是面向对象中的一个重要概念。一般来讲,一个类只能继承自另一个类,有时候不同类之间可以有一些共有的特性,这时候就可以把特性提取成接口(interfaces),用 implements 关键字来实现。这个特性大大提高了面向对象的灵活性。

  1. interface Alarm {
  2. alert(): void;
  3. }
  4. class Door {
  5. }
  6. class SecurityDoor extends Door implements Alarm {
  7. alert() {
  8. console.log('SecurityDoor alert');
  9. }
  10. }
  11. class Car implements Alarm {
  12. alert() {
  13. console.log('Car alert');
  14. }
  15. }
  16. //一个类可以实现多个接口
  17. interface Alarm {
  18. alert(): void;
  19. }
  20. interface Light {
  21. lightOn(): void;
  22. lightOff(): void;
  23. }
  24. class Car implements Alarm, Light {
  25. alert() {
  26. console.log('Car alert');
  27. }
  28. lightOn() {
  29. console.log('Car light on');
  30. }
  31. lightOff() {
  32. console.log('Car light off');
  33. }
  34. }

2.接口继承接口

  1. interface Alarm {
  2. alert(): void;
  3. }
  4. interface LightableAlarm extends Alarm {
  5. lightOn(): void;
  6. lightOff(): void;
  7. }

3.接口继承类

  1. class Point {
  2. x: number;
  3. y: number;
  4. constructor(x: number, y: number) {
  5. this.x = x;
  6. this.y = y;
  7. }
  8. }
  9. interface Point3d extends Point {
  10. z: number;
  11. }
  12. let point3d: Point3d = {x: 1, y: 2, z: 3};

7.泛型

泛型(Generics)是指在定义函数、接口或类的时候,不预先指定具体的类型,而在使用的时候再指定类型的一种特性。

  1. //定义时未指定返回值类型,而根据输入的参数value的类型自动推断
  2. function createArray<T>(length: number, value: T): Array<T> {
  3. let result: T[] = [];
  4. for (let i = 0; i < length; i++) {
  5. result[i] = value;
  6. }
  7. return result;
  8. }
  9. createArray<string>(3, 'x'); // ['x', 'x', 'x']