title: 类型保护

类型保护

原文地址

在本教程中,你将学习 TypeScript 中的类型保护。

使用 条件代码块 限定变量类型的范围,达到类型保护的目的。

typeof

看看下面的例子:

  1. type alphanumeric = string | number;
  2. function add(a: alphanumeric, b: alphanumeric) {
  3. if (typeof a === 'number' && typeof b === 'number') {
  4. return a + b;
  5. }
  6. if (typeof a === 'string' && typeof b === 'string') {
  7. return a.concat(b);
  8. }
  9. throw new Error(
  10. 'Invalid arguments. Both arguments must be either numbers or strings.',
  11. );
  12. }

它是这样工作的:

  • 首先定义了 alphanumeric 类型,它可以保存 字符串类型 或者 数字类型 的值;
  • 接下来,定义了 add() 函数,它把类型为 alphanumericab 变量的值进行相加;
  • 然后使用 typeof 操作符检查两个参数的类型是否都为 number 类型,如果是的话,使用 + 操作符计算参数值之和;
  • 再然后使用 typeof 操作符检查两个参数的类型是否都为 string 类型,如果是的话,把两个字符串参数值拼接起来;
  • 最后,如果两个参数不都是数字类型或者不都是字符串类型的话,抛出错误提示。

在这个例子中,TypeScript 知道如何在条件代码块中使用 typeof 操作符。

在下面的 if 语句中,TypeScript 认为 ab 变量都是数字类型:

  1. if (typeof a === 'number' && typeof b === 'number') {
  2. return a + b;
  3. }

类似地,在下面的 if 语句中,TypeScript 将 ab 变量作为字符串来处理,因此,可以把它们拼接成一个字符串:

  1. if (typeof a === 'string' && typeof b === 'string') {
  2. return a.concat(b);
  3. }

instanceof

typeof 操作符类似,TypeScript 也知道如何使用 instanceof 操作符,如下所示:

  1. class Customer {
  2. isCreditAllowed(): boolean {
  3. // ...
  4. return true;
  5. }
  6. }
  7. class Supplier {
  8. isInShortList(): boolean {
  9. // ...
  10. return true;
  11. }
  12. }
  13. type BusinessPartner = Customer | Supplier;
  14. function signContract(partner: BusinessPartner): string {
  15. let message: string;
  16. if (partner instanceof Customer) {
  17. message = partner.isCreditAllowed()
  18. ? 'Sign a new contract with the customer'
  19. : 'Credit issue';
  20. }
  21. if (partner instanceof Supplier) {
  22. message = partner.isInShortList()
  23. ? 'Sign a new contract the supplier'
  24. : 'Need to evaluate further';
  25. }
  26. return message;
  27. }

它这样何工作的:

  • 首先,声明了 CustomerSupplier 两个类;
  • 第二, 创建类型别名为 BusinessPartner 的类型,它是CustomerSupplier 的联合类型;
  • 第三,定义 signContract() 函数,它接受一个类型为 BusinessPartner 的参数;
  • 最后,检查 partner 是否为 Customer 或者 Supplier 类的实例,然后进行对应的逻辑处理。

在下面的 if 代码块中,TypeScript 通过 instanceof 操作符知道 partnerCustomer 类型的一个实例:

  1. if (partner instanceof Customer) {
  2. message = partner.isCreditAllowed()
  3. ? 'Sign a new contract with the customer'
  4. : 'Credit issue';
  5. }

同样的方式,在下面的 if 代码块中,TypeScript 知道 partnerSupplier 类型的一个实例:

  1. if (partner instanceof Supplier) {
  2. message = partner.isInShortList()
  3. ? 'Sign a new contract with the supplier'
  4. : 'Need to evaluate further';
  5. }

if 语句限定了一种类型,TypeScript 知道在 else 语句中会是另外一种类型,如下所示:

  1. function signContract(partner: BusinessPartner): string {
  2. let message: string;
  3. if (partner instanceof Customer) {
  4. message = partner.isCreditAllowed()
  5. ? 'Sign a new contract with the customer'
  6. : 'Credit issue';
  7. } else {
  8. // must be Supplier
  9. message = partner.isInShortList()
  10. ? 'Sign a new contract with the supplier'
  11. : 'Need to evaluate further';
  12. }
  13. return message;
  14. }

in

in 操作符通过判断对象上是否存在某个属性来进行安全检查,也可以将它用作类型保护,如下所示:

  1. function signContract(partner: BusinessPartner): string {
  2. let message: string;
  3. if ('isCreditAllowed' in partner) {
  4. message = partner.isCreditAllowed()
  5. ? 'Sign a new contract with the customer'
  6. : 'Credit issue';
  7. } else {
  8. // must be Supplier
  9. message = partner.isInShortList()
  10. ? 'Sign a new contract the supplier '
  11. : 'Need to evaluate further';
  12. }
  13. return message;
  14. }

用户自定义的类型保护

用户自定义的类型保护允许你使用函数的时候定义类型保护或者帮助 TypeScript 推断类型。用户自定义的类型保护函数是一个返回 arg is aType 判断的函数,如下所示:

  1. function isCustomer(partner: any): partner is Customer {
  2. return partner instanceof Customer;
  3. }

在这个例子中,isCustomer() 是一个用户自定义的类型保护函数,可以按照下面的例子来使用它:

  1. function signContract(partner: BusinessPartner): string {
  2. let message: string;
  3. if (isCustomer(partner)) {
  4. message = partner.isCreditAllowed()
  5. ? 'Sign a new contract with the customer'
  6. : 'Credit issue';
  7. } else {
  8. message = partner.isInShortList()
  9. ? 'Sign a new contract with the supplier'
  10. : 'Need to evaluate further';
  11. }
  12. return message;
  13. }

小结

  • 类型保护限定了条件语句中变量的类型;
  • 使用 typeofinstanceof 操作符在条件语句中实现类型保护。