引言

作为一门强大的静态类型检查工具,如今在许多中大型应用程序以及流行的JS库中均能看到TypeScript的身影。JS作为一门弱类型语言,在我们写代码的过程中稍不留神便会修改掉变量的类型,从而导致一些出乎意料的运行时错误。然而TypeScript在编译过程中便能帮我们解决这个难题,不仅在JS中引入了强类型检查,并且编译后的JS代码能够运行在任何浏览器环境,Node环境和任何支持ECMAScript 3(或更高版本)的JS引擎中。最近公司刚好准备使用TypeScript来对现有系统进行重构,以前使用TypeScript的机会也不多,特别是一些有用的高级用法,所以借着这次机会,重新巩固夯实一下这方面的知识点,如果有错误的地方,还请指出。

1、类继承

在ES5中,我们一般通过函数或者基于原型的继承来封装一些组件公共的部分方便复用,然而在TypeScript中,我们可以像类似Java语言中以面向对象的方式使用类继承来创建可复用的组件。我们可以通过class关键字来创建类,并基于它使用new操作符来实例化一个对象。为了将多个类的公共部分进行抽象,我们可以创建一个父类并让子类通过extends关键字来继承父类,从而减少一些冗余代码的编写增加代码的可复用性和可维护性。示例如下:

  1. class Parent {
  2. readonly x: number;
  3. constructor() {
  4. this.x = 1;
  5. }
  6. print() {
  7. console.log(this.x);
  8. }
  9. }
  10. class Child extends Parent {
  11. readonly y: number;
  12. constructor() {
  13. // 注意此处必须优先调用super()方法
  14. super();
  15. this.y = 2;
  16. }
  17. print() {
  18. // 通过super调用父类原型上的方法,但是方法中的this指向的是子类的实例
  19. super.print();
  20. console.log(this.y);
  21. }
  22. }
  23. const child = new Child();
  24. console.log(child.print()) // -> 1 2

在上述示例中,Child子类中对父类的print方法进行重写,同时在内部使用super.print()来调用父类的公共逻辑,从而实现逻辑复用。class关键字作为构造函数的语法糖,在经过TypeScript编译后,最终会被转换为兼容性好的浏览器可识别的ES5代码。class在面向对象的编程范式中非常常见,因此为了弄清楚其背后的实现机制,我们不妨多花点时间来看下经过编译转换之后的代码是什么样子的(当然这部分已经比较熟悉的同学可以直接跳过)。

  1. var __extends = (this && this.__extends) || (function () {
  2. var extendStatics = function (d, b) {
  3. extendStatics = Object.setPrototypeOf ||
  4. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  5. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  6. return extendStatics(d, b);
  7. }
  8. return function (d, b) {
  9. extendStatics(d, b);
  10. function __() { this.constructor = d; }
  11. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  12. };
  13. })();
  14. var Parent = /** @class */ (function () {
  15. function Parent() {
  16. this.x = 1;
  17. }
  18. Parent.prototype.print = function () {
  19. console.log(this.x);
  20. };
  21. return Parent;
  22. }());
  23. var Child = /** @class */ (function (_super) {
  24. __extends(Child, _super);
  25. function Child() {
  26. var _this =
  27. // 注意此处必须优先调用super()方法
  28. _super.call(this) || this;
  29. _this.y = 2;
  30. return _this;
  31. }
  32. Child.prototype.print = function () {
  33. // 通过super调用父类原型上的方法,但是方法中的this指向的是子类的实例
  34. _super.prototype.print.call(this);
  35. console.log(this.y);
  36. };
  37. return Child;
  38. }(Parent));
  39. var child = new Child();
  40. console.log(child.print()); // -> 1 2

以上就是转换后的完整代码,为了方便对比,这里将原来的注释信息保留,仔细研究这段代码我们会发现以下几个要点:

  1. 子类Child的构造函数中super()方法被转换成了var _this = _super.call(this) || this,这里的_super指的就是父类Parent,因此这句代码的含义就是调用父类构造函数并将this绑定到子类的实例上,这样的话子类实例便可拥有父类的x属性。因此为了实现属性继承,我们必须在子类构造函数中调用super()方法,如果不调用会编译不通过。

  2. 子类Childprint方法中super.print()方法被转换成了_super.prototype.print.call(this),这句代码的含义就是调用父类原型上的print方法并将方法中的this指向子类实例,由于在上一步操作中我们已经继承到父类的x属性,因此这里我们将直接打印出子类实例的x属性的值。

  3. extends关键字最终被转换为__extends(Child, _super)方法,其中_super指的是父类Parent,为了方便查看,这里将_extends方法单独提出来进行研究。

  1. var __extends = (this && this.__extends) || (function () {
  2. var extendStatics = function (d, b) {
  3. extendStatics = Object.setPrototypeOf ||
  4. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  5. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  6. return extendStatics(d, b);
  7. }
  8. return function (d, b) {
  9. // 第一部分
  10. extendStatics(d, b);
  11. // 第二部分
  12. function __() { this.constructor = d; }
  13. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  14. };
  15. })();

在以上代码中,主要可以分为两个部分来进行理解,第一部分为extendStatics(d, b)方法,第二部分为该方法后面的两行代码。

第一部分

extendStatics方法内部虽然代码量相对较多,但是不难发现其实还是主要为了兼容ES5版本的执行环境。在ES6中新增了Object.setPrototypeOf方法用于手动设置对象的原型,但是在ES5的环境中我们一般通过一个非标准的__proto__属性来进行设置,Object.setPrototypeOf方法的原理其实也是通过该属性来设置对象的原型,其实现方式如下:

  1. Object.setPrototypeOf = function(obj, proto) {
  2. obj.__proto__ = proto;
  3. return obj;
  4. }

extendStatics(d, b)方法中,d指子类Childb指父类Parent,因此该方法的作用可以解释为:

  1. // 将子类Child的__proto__属性指向父类Parent
  2. Child.__proto__ = Parent;

可以将这行代码理解为构造函数的继承,或者叫静态属性和静态方法的继承,即属性和方法不是挂载到构造函数的prototype原型上的,而是直接挂载到构造函数本身,因为在JS中函数本身也可以作为一个对象,并可以为其赋予任何其他的属性,示例如下:

  1. function Foo() {
  2. this.x = 1;
  3. this.y = 2;
  4. }
  5. Foo.bar = function() {
  6. console.log(3);
  7. }
  8. Foo.baz = 4;
  9. console.log(Foo.bar()) // -> 3
  10. console.log(Foo.baz) // -> 4

因此当我们在子类Child中以Child.someProperty访问属性时,如果子类中不存在就会通过Child.__proto__寻找父类的同名属性,通过这种方式来实现静态属性和静态方法的路径查找。

第二部分

在第二部分中仅包含以下两行代码:

  1. function __() { this.constructor = d; }
  2. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());

其中d指子类Childb指父类Parent,这里对于JS中实现继承的几种方式比较熟悉的同学可以一眼看出,这里使用了寄生组合式继承的方式,通过借用一个中间函数__()来避免当修改子类的prototype上的方法时对父类的prototype所造成的影响。我们知道,在JS中通过构造函数实例化一个对象之后,该对象会拥有一个__proto__属性并指向其构造函数的prototype属性,示例如下:

  1. function Foo() {
  2. this.x = 1;
  3. this.y = 2;
  4. }
  5. const foo = new Foo();
  6. foo.__proto__ === Foo.prototype; // -> true

对于本例中,如果通过子类Child来实例化一个对象之后,会产生如下关联:

  1. const child = new Child();
  2. child.__proto__ === (Child.prototype = new __());
  3. child.__proto__.__proto__ === __.prototype === Parent.prototype;
  4. // 上述代码等价于下面这种方式
  5. Child.prototype.__proto__ === Parent.prototype;

因此当我们在子类Child的实例child对象中通过child.someMethod()调用某个方法时,如果在实例中不存在该方法,则会沿着__proto__继续往上查找,最终会经过父类Parentprototype原型,即通过这种方式来实现方法的继承。

基于对以上两个部分的分析,我们可以总结出以下两点:

  1. // 表示构造函数的继承,或者叫做静态属性和静态方法的继承,总是指向父类
  2. 1. Child.__proto__ === Parent;
  3. // 表示方法的继承,总是指向父类的prototype属性
  4. 2. Child.prototype.__proto__ === Parent.prototype;

2、访问修饰符

TypeScript为我们提供了访问修饰符(Access Modifiers)来限制在class外部对内部属性的访问,访问修饰符主要包含以下三种:

  • public:公共修饰符,其修饰的属性和方法都是公有的,可以在任何地方被访问到,默认情况下所有属性和方法都是public的。
  • private:私有修饰符,其修饰的属性和方法在class外部不可见。
  • protected:受保护修饰符,和private比较相似,但是其修饰的属性和方法在子类内部是被允许访问的

我们通过一些示例来对几种修饰符进行对比:

  1. class Human {
  2. public name: string;
  3. public age: number;
  4. public constructor(name: string, age: number) {
  5. this.name = name;
  6. this.age = age;
  7. }
  8. }
  9. const man = new Human('tom', 20);
  10. console.log(man.name, man.age); // -> tom 20
  11. man.age = 21;
  12. console.log(man.age); // -> 21

在上述示例中,由于我们将访问修饰符设置为public,因此我们通过实例man来访问nameage属性是被允许的,同时对age属性重新赋值也是允许的。但是在某些情况下,我们希望某些属性是对外不可见的,同时不允许被修改,那么我们就可以使用private修饰符:

  1. class Human {
  2. public name: string;
  3. private age: number; // 此处修改为使用private修饰符
  4. public constructor(name: string, age: number) {
  5. this.name = name;
  6. this.age = age;
  7. }
  8. }
  9. const man = new Human('tom', 20);
  10. console.log(man.name); // -> tom
  11. console.log(man.age);
  12. // -> Property 'age' is private and only accessible within class 'Human'.

我们将age属性的修饰符修改为private后,在外部通过man.age对其进行访问,TypeScript在编译阶段就会发现其是一个私有属性并最终将会报错。

注意:在TypeScript编译之后的代码中并没有限制对私有属性的存取操作。

编译后的代码如下:

  1. var Human = /** @class */ (function () {
  2. function Human(name, age) {
  3. this.name = name;
  4. this.age = age;
  5. }
  6. return Human;
  7. }());
  8. var man = new Human('tom', 20);
  9. console.log(man.name); // -> tom
  10. console.log(man.age); // -> 20

使用private修饰符修饰的属性或者方法在子类中也是不允许访问的,示例如下:

  1. class Human {
  2. public name: string;
  3. private age: number;
  4. public constructor(name: string, age: number) {
  5. this.name = name;
  6. this.age = age;
  7. }
  8. }
  9. class Woman extends Human {
  10. private gender: number = 0;
  11. public constructor(name: string, age: number) {
  12. super(name, age);
  13. console.log(this.age);
  14. }
  15. }
  16. const woman = new Woman('Alice', 18);
  17. // -> Property 'age' is private and only accessible within class 'Human'.

在上述示例中由于在父类Humanage属性被设置为private,因此在子类Woman中无法访问到age属性,为了让在子类中允许访问age属性,我们可以使用protected修饰符来对其进行修饰:

  1. class Human {
  2. public name: string;
  3. protected age: number; // 此处修改为使用protected修饰符
  4. public constructor(name: string, age: number) {
  5. this.name = name;
  6. this.age = age;
  7. }
  8. }
  9. class Woman extends Human {
  10. private gender: number = 0;
  11. public constructor(name: string, age: number) {
  12. super(name, age);
  13. console.log(this.age);
  14. }
  15. }
  16. const woman = new Woman('Alice', 18); // -> 18

当我们将private修饰符用于构造函数时,则表示该类不允许被继承或实例化,示例如下:

  1. class Human {
  2. public name: string;
  3. public age: number;
  4. // 此处修改为使用private修饰符
  5. private constructor(name: string, age: number) {
  6. this.name = name;
  7. this.age = age;
  8. }
  9. }
  10. class Woman extends Human {
  11. private gender: number = 0;
  12. public constructor(name: string, age: number) {
  13. super(name, age);
  14. }
  15. }
  16. const man = new Human('Alice', 18);
  17. // -> Cannot extend a class 'Human'. Class constructor is marked as private.
  18. // -> Constructor of class 'Human' is private and only accessible within the class declaration.

当我们将protected修饰符用于构造函数时,则表示该类只允许被继承,示例如下:

  1. class Human {
  2. public name: string;
  3. public age: number;
  4. // 此处修改为使用protected修饰符
  5. protected constructor(name: string, age: number) {
  6. this.name = name;
  7. this.age = age;
  8. }
  9. }
  10. class Woman extends Human {
  11. private gender: number = 0;
  12. public constructor(name: string, age: number) {
  13. super(name, age);
  14. }
  15. }
  16. const man = new Human('Alice', 18);
  17. // -> Constructor of class 'Human' is protected and only accessible within the class declaration.

另外我们还可以直接将修饰符放到构造函数的参数中,示例如下:

  1. class Human {
  2. // public name: string;
  3. // private age: number;
  4. public constructor(public name: string, private age: number) {
  5. this.name = name;
  6. this.age = age;
  7. }
  8. }
  9. const man = new Human('tom', 20);
  10. console.log(man.name); // -> tom
  11. console.log(man.age);
  12. // -> Property 'age' is private and only accessible within class 'Human'.

3、接口与构造器签名

当我们的项目中拥有很多不同的类时并且这些类之间可能存在某方面的共同点,为了描述这种共同点,我们可以将其提取到一个接口(interface)中用于集中维护,并使用implements关键字来实现这个接口,示例如下:

  1. interface IHuman {
  2. name: string;
  3. age: number;
  4. walk(): void;
  5. }
  6. class Human implements IHuman {
  7. public constructor(public name: string, public age: number) {
  8. this.name = name;
  9. this.age = age;
  10. }
  11. walk(): void {
  12. console.log('I am walking...');
  13. }
  14. }

上述代码在编译阶段能顺利通过,但是我们注意到在Human类中包含constructor构造函数,如果我们想在接口中为该构造函数定义一个签名并让Human类来实现这个接口,看会发生什么:

  1. interface HumanConstructor {
  2. new (name: string, age: number);
  3. }
  4. class Human implements HumanConstructor {
  5. public constructor(public name: string, public age: number) {
  6. this.name = name;
  7. this.age = age;
  8. }
  9. walk(): void {
  10. console.log('I am walking...');
  11. }
  12. }
  13. // -> Class 'Human' incorrectly implements interface 'HumanConstructor'.
  14. // -> Type 'Human' provides no match for the signature 'new (name: string, age: number): any'.

然而TypeScript会编译出错,告诉我们错误地实现了HumanConstructor接口,这是因为当一个类实现一个接口时,只会对实例部分进行编译检查,类的静态部分是不会被编译器检查的。因此这里我们尝试换种方式,直接操作类的静态部分,示例如下:

  1. interface HumanConstructor {
  2. new (name: string, age: number);
  3. }
  4. interface IHuman {
  5. name: string;
  6. age: number;
  7. walk(): void;
  8. }
  9. class Human implements IHuman {
  10. public constructor(public name: string, public age: number) {
  11. this.name = name;
  12. this.age = age;
  13. }
  14. walk(): void {
  15. console.log('I am walking...');
  16. }
  17. }
  18. // 定义一个工厂方法
  19. function createHuman(constructor: HumanConstructor, name: string, age: number): IHuman {
  20. return new constructor(name, age);
  21. }
  22. const man = createHuman(Human, 'tom', 18);
  23. console.log(man.name, man.age); // -> tom 18

在上述示例中通过额外创建一个工厂方法createHuman并将构造函数作为第一个参数传入,此时当我们调用createHuman(Human, 'tom', 18)时编译器便会检查第一个参数是否符合HumanConstructor接口的构造器签名。

4、声明合并

在声明合并中最常见的合并类型就是接口了,因此这里先从接口开始介绍几种比较常见的合并方式。

4.1 接口合并

示例代码如下:

  1. interface A {
  2. name: string;
  3. }
  4. interface A {
  5. age: number;
  6. }
  7. // 等价于
  8. interface A {
  9. name: string;
  10. age: number;
  11. }
  12. const a: A = {name: 'tom', age: 18};

接口合并的方式比较容易理解,即声明多个同名的接口,每个接口中包含不同的属性声明,最终这些来自多个接口的属性声明会被合并到同一个接口中。

注意:所有同名接口中的非函数成员必须唯一,如果不唯一则必须保证类型相同,否则编译器会报错。对于函数成员,后声明的同名接口会覆盖掉之前声明的同名接口,即后声明的同名接口中的函数相当于一次重载,具有更高的优先级。

4.2 函数合并

函数的合并可以简单理解为函数的重载,即通过同时定义多个不同类型参数或不同类型返回值的同名函数来实现,示例代码如下:

  1. // 函数定义
  2. function foo(x: number): number;
  3. function foo(x: string): string;
  4. // 函数具体实现
  5. function foo(x: number | string): number | string {
  6. if (typeof x === 'number') {
  7. return (x).toFixed(2);
  8. }
  9. return x.substring(0, x.length - 1);
  10. }

在上述示例中,我们对foo函数进行多次定义,每次定义的函数参数类型不同,返回值类型不同,最后一次为函数的具体实现,在实现中只有在兼容到前面的所有定义时,编译器才不会报错。

注意:TypeScript编译器会优先从最开始的函数定义进行匹配,因此如果多个函数定义存在包含关系,则需要将最精确的函数定义放到最前面,否则将始终不会被匹配到。

4.3 类型别名联合

类型别名联合与接口合并有所区别,类型别名不会新建一个类型,只是创建一个新的别名来对多个类型进行引用,同时不能像接口一样被实现(implements)继承(extends),示例如下:

  1. type HumanProperty = {
  2. name: string;
  3. age: number;
  4. gender: number;
  5. };
  6. type HumanBehavior = {
  7. eat(): void;
  8. walk(): void;
  9. }
  10. type Human = HumanProperty & HumanBehavior;
  11. let woman: Human = {
  12. name: 'tom',
  13. age: 18,
  14. gender: 0,
  15. eat() {
  16. console.log('I can eat.');
  17. },
  18. walk() {
  19. console.log('I can walk.');
  20. }
  21. }
  22. class HumanComponent extends Human {
  23. constructor(public name: string, public age: number, public gender: number) {
  24. this.name = name;
  25. this.age = age;
  26. this.gender = gender;
  27. }
  28. eat() {
  29. console.log('I can eat.');
  30. }
  31. walk() {
  32. console.log('I can walk.');
  33. }
  34. }
  35. // -> 'Human' only refers to a type, but is being used as a value here.

5、keyof 索引查询

在TypeScript中的keyof有点类似于JS中的Object.keys()方法,但是区别在于前者遍历的是类型中的字符串索引,后者遍历的是对象中的键名,示例如下:

  1. interface Rectangle {
  2. x: number;
  3. y: number;
  4. width: number;
  5. height: number;
  6. }
  7. type keys = keyof Rectangle;
  8. // 等价于
  9. type keys = "x" | "y" | "width" | "height";
  10. // 这里使用了泛型,强制要求第二个参数的参数名必须包含在第一个参数的所有字符串索引中
  11. function getRectProperty<T extends object, K extends keyof T>(rect: T, property: K): T[K] {
  12. return rect[property];
  13. }
  14. let rect: Rectangle = {
  15. x: 50,
  16. y: 50,
  17. width: 100,
  18. height: 200
  19. };
  20. console.log(getRectProperty(rect, 'width')); // -> 100
  21. console.log(getRectProperty(rect, 'notExist'));
  22. // -> Argument of type '"notExist"' is not assignable to parameter of type '"width" | "x" | "y" | "height"'.

在上述示例中我们通过使用keyof来限制函数的参数名property必须被包含在类型Rectangle的所有字符串索引中,如果没有被包含则编译器会报错,可以用来在编译时检测对象的属性名是否书写有误。

6、Partial 可选属性

在某些情况下,我们希望类型中的所有属性都不是必需的,只有在某些条件下才存在,我们就可以使用Partial来将已声明的类型中的所有属性标识为可选的,示例如下:

  1. // 该类型已内置在TypeScript中
  2. type Partial<T> = {
  3. [P in keyof T]?: T[P]
  4. };
  5. interface Rectangle {
  6. x: number;
  7. y: number;
  8. width: number;
  9. height: number;
  10. }
  11. type PartialRectangle = Partial<Rectangle>;
  12. // 等价于
  13. type PartialRectangle = {
  14. x?: number;
  15. y?: number;
  16. width?: number;
  17. height?: number;
  18. }
  19. let rect: PartialRectangle = {
  20. width: 100,
  21. height: 200
  22. };

在上述示例中由于我们使用Partial将所有属性标识为可选的,因此最终rect对象中虽然只包含widthheight属性,但是编译器依旧没有报错,当我们不能明确地确定对象中包含哪些属性时,我们就可以通过Partial来声明。

7、Pick 部分选择

在某些应用场景下,我们可能需要从一个已声明的类型中抽取出一个子类型,在子类型中包含父类型中的部分或全部属性,这时我们可以使用Pick来实现,示例代码如下:

  1. // 该类型已内置在TypeScript中
  2. type Pick<T, K extends keyof T> = {
  3. [P in K]: T[P]
  4. };
  5. interface User {
  6. id: number;
  7. name: string;
  8. age: number;
  9. gender: number;
  10. email: string;
  11. }
  12. type PickUser = Pick<User, "id" | "name" | "gender">;
  13. // 等价于
  14. type PickUser = {
  15. id: number;
  16. name: string;
  17. gender: number;
  18. };
  19. let user: PickUser = {
  20. id: 1,
  21. name: 'tom',
  22. gender: 1
  23. };

在上述示例中,由于我们只关心user对象中的idnamegender是否存在,其他属性不做明确规定,因此我们就可以使用PickUser接口中拣选出我们关心的属性而忽略其他属性的编译检查。

8、never 永不存在

never表示的是那些永不存在的值的类型,比如在函数中抛出异常或者无限循环,never类型可以是任何类型的子类型,也可以赋值给任何类型,但是相反却没有一个类型可以作为never类型的子类型,示例如下:

  1. // 函数抛出异常
  2. function throwError(message: string): never {
  3. throw new Error(message);
  4. }
  5. // 函数自动推断出返回值为never类型
  6. function reportError(message: string) {
  7. return throwError(message);
  8. }
  9. // 无限循环
  10. function loop(): never {
  11. while(true) {
  12. console.log(1);
  13. }
  14. }
  15. // never类型可以是任何类型的子类型
  16. let n: never;
  17. let a: string = n;
  18. let b: number = n;
  19. let c: boolean = n;
  20. let d: null = n;
  21. let e: undefined = n;
  22. let f: any = n;
  23. // 任何类型都不能赋值给never类型
  24. let a: string = '123';
  25. let b: number = 0;
  26. let c: boolean = true;
  27. let d: null = null;
  28. let e: undefined = undefined;
  29. let f: any = [];
  30. let n: never = a;
  31. // -> Type 'string' is not assignable to type 'never'.
  32. let n: never = b;
  33. // -> Type 'number' is not assignable to type 'never'.
  34. let n: never = c;
  35. // -> Type 'true' is not assignable to type 'never'.
  36. let n: never = d;
  37. // -> Type 'null' is not assignable to type 'never'.
  38. let n: never = e;
  39. // -> Type 'undefined' is not assignable to type 'never'.
  40. let n: never = f;
  41. // -> Type 'any' is not assignable to type 'never'.

9、Exclude 属性排除

Pick相反,Pick用于拣选出我们需要关心的属性,而Exclude用于排除掉我们不需要关心的属性,示例如下:

  1. // 该类型已内置在TypeScript中
  2. // 这里使用了条件类型(Conditional Type),和JS中的三目运算符效果一致
  3. type Exclude<T, U> = T extends U ? never : T;
  4. interface User {
  5. id: number;
  6. name: string;
  7. age: number;
  8. gender: number;
  9. email: string;
  10. }
  11. type keys = keyof User; // -> "id" | "name" | "age" | "gender" | "email"
  12. type ExcludeUser = Exclude<keys, "age" | "email">;
  13. // 等价于
  14. type ExcludeUser = "id" | "name" | "gender";

在上述示例中我们通过在ExcludeUser中传入我们不需要关心的ageemail属性,Exclude会帮助我们将不需要的属性进行剔除,留下的属性idnamegender即为我们需要关心的属性。一般来说,Exclude很少单独使用,可以与其他类型配合实现更复杂更有用的功能。

10、Omit 属性忽略

在上一个用法中,我们使用Exclude来排除掉其他不需要的属性,但是在上述示例中的写法耦合度较高,当有其他类型也需要这样处理时,就必须再实现一遍相同的逻辑,不妨我们再进一步封装,隐藏这些底层的处理细节,只对外暴露简单的公共接口,示例如下:

  1. // 使用Pick和Exclude组合实现
  2. type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
  3. interface User {
  4. id: number;
  5. name: string;
  6. age: number;
  7. gender: number;
  8. email: string;
  9. }
  10. // 表示忽略掉User接口中的age和email属性
  11. type OmitUser = Omit<User, "age" | "email">;
  12. // 等价于
  13. type OmitUser = {
  14. id: number;
  15. name: string;
  16. gender: number;
  17. };
  18. let user: OmitUser = {
  19. id: 1,
  20. name: 'tom',
  21. gender: 1
  22. };

在上述示例中,我们需要忽略掉User接口中的ageemail属性,则只需要将接口名和属性传入Omit即可,对于其他类型也是如此,大大提高了类型的可扩展能力,方便复用。

总结

在本文中总结了几种TypeScript的使用技巧,如果在我们的TypeScript项目中发现有很多类型声明的地方具有共性,那么不妨可以使用文中的几种技巧来对其进行优化改善,增加代码的可维护性和可复用性。笔者之前使用TypeScript的机会也不多,所以最近也是一边学习一边总结,如果文中有错误的地方,还希望能够在评论区指正。

交流

如果你觉得这篇文章的内容对你有帮助,能否帮个忙关注一下笔者的公众号[前端之境],每周都会努力原创一些前端技术干货,关注公众号后可以邀你加入前端技术交流群,我们可以一起互相交流,共同进步。

文章已同步更新至Github博客,若觉文章尚可,欢迎前往star!

你的一个点赞,值得让我付出更多的努力!

逆境中成长,只有不断地学习,才能成为更好的自己,与君共勉!

TypeScript高级用法详解的更多相关文章

  1. Python中第三方库Requests库的高级用法详解

    Python中第三方库Requests库的高级用法详解 虽然Python的标准库中urllib2模块已经包含了平常我们使用的大多数功能,但是它的API使用起来让人实在感觉不好.它已经不适合现在的时代, ...

  2. PHP switch的“高级”用法详解

    只所以称为“高级”用法,是因为我连switch的最基础的用法都还没有掌握,so,接下来讲的其实还是它的基础用法! switch 语句和具有同样表达式的一系列的 IF 语句相似.很多场合下需要把同一个变 ...

  3. shell中echo基础及高级用法详解-渐入佳境

    --作者:飞翔的小胖猪 --创建时间:2021年2月19日 1.1 基础用法 echo命令用来输出文本,在shell脚本中用来输出提示信息用的比较多. 单引号:原样输出所有的内容,不用转义就能输出特殊 ...

  4. jQuery动画高级用法——详解animation中的.queue()函数

    http://www.cnblogs.com/zhwl/p/4328279.html $('#object').hide('slow').queue(function(next){     $(thi ...

  5. Linux下find命令用法详解

    Linux下find命令用法详解   学神VIP烟火 学神IT教育:XueGod-IT   最负责任的线上直播教育平台   本文作者为VIP学员 烟火   第一部分:根据文件名查找   1.在当前目录 ...

  6. DAX/PowerBI系列 - 查询参数用法详解(Query Parameter)

    PowerBI  - 查询参数用法详解(Query Parameter) 很多人都不知道查询参数用来干啥,下面总结一下日常项目中常用的几个查询参数的地方.(本人不太欢hardcode的东西) 使用查询 ...

  7. Python中的高级数据结构详解

    这篇文章主要介绍了Python中的高级数据结构详解,本文讲解了Collection.Array.Heapq.Bisect.Weakref.Copy以及Pprint这些数据结构的用法,需要的朋友可以参考 ...

  8. new String(str.getBytes(“gbk”),“gbk”)的用法详解

    new String(str.getBytes(“gbk”),“gbk”)的用法详解 前提是str存放的是汉字 一.如果是new String(str.getBytes(“gbk”),“gbk”)时, ...

  9. (转)Linux命令之Ethtool用法详解

    Linux命令之Ethtool用法详解 原文:http://www.linuxidc.com/Linux/2012-01/52669.htm Linux/Unix命令之Ethtool描述:Ethtoo ...

随机推荐

  1. 微擎框架商业版 V2.1.2 去后门一键安装版+去除云平台+无附带模块

    下载地址:http://dd.ma/AdVvoDu5 关注微信公众号codervip,点击公众号菜单,获取提取码! 这个是一键安装版本,所以微擎安装比较简单,不用大家手动去改数据库了,而且修复上个2. ...

  2. pat 1100 Mars Numbers(20 分)

    1100 Mars Numbers(20 分) People on Mars count their numbers with base 13: Zero on Earth is called &qu ...

  3. lqb 入门训练 序列求和 (PS:用长整数做数据的输入输出)

    入门训练 序列求和 时间限制:1.0s   内存限制:256.0MB     问题描述 求1+2+3+...+n的值. 输入格式 输入包括一个整数n. 输出格式 输出一行,包括一个整数,表示1+2+3 ...

  4. 领扣(LeetCode)字符串相加 个人题解

    给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和. 注意: num1 和num2 的长度都小于 5100. num1 和num2 都只包含数字 0-9. num1 和num2 都不包 ...

  5. bash6——循环

    for fruit in apple orange pear #写死 do each ${fruit}s done fruits="apple orange pear" #输入变量 ...

  6. selenium中延时等待三种方式

    selenium中的延时等待方式有三种:强制等待:sleep()  隐示等待:implicitly_wait()  显示等待 WebDriverWait() 1.强制等待:sleep(),time模块 ...

  7. ArcGIS 用QueryTask查询上限1000的问题

    1.打开ArcGIS Server找到自己发布的服务,右键Service Properties,左侧点击Parameters,右侧有一个Maximum number of records return ...

  8. Java中的继承、封装、多态的理解

    Java中的继承.封装.多态 继承的理解: 1.继承是面向对象的三大特征之一,也是实现代码复用的重要手段.Java的继承具有单继承的特点,每个子类只有一个直接父类. 2.Java的继承通过extend ...

  9. 驰骋工作流系统-Java共工作流引擎配置定时任务

    关键词:工作流定时任务  流程引擎定时任务设置  工作流系统定时任务配置  开源工作流引擎 开源工作流系统 一.定时任务的作用 发送邮件,发送短信. 处理节点自动执行的任务.比如:一个节点的待办工作是 ...

  10. (四十四)golang--协程(goroutine)和管道(channel)相结合实例

    统计1-8000之间的素数. 整体框架: 说明:有五个协程,三个管道.其中一个协程用于写入数字到intChan管道中,另外四个用于取出intChan管道中的数字并判断是否是素数,然后将素数写入到pri ...