「TypeScript」类型守卫(TypeGuard)🍃

一、简介

 TypeScript中的类型守卫(Type Guards)是一种技术,它可以用于在代码中更明确地指定变量的类型。这是TypeScript提供的一种强大功能,因为它可以帮助开发者避免类型错误,同时还能利用TypeScript的类型系统来提供更好的代码智能提示和自动补全。类型守卫主要用于告诉TypeScript编译器,某个变量在特定的代码块中是特定的类型。

二、使用场景

 类型守卫一般要配合if语句,在if语句创建的块级作用域中收窄对应变量的类型。当然也可以使用三目运算符、逻辑与运算符等使用。

1. 区分联合类型

typescript 复制代码
function process(input: string | number) {
  if (typeof input === "string") {
    // do something...
  } else {
    // do something...
  }
}

2. 收窄类型

typescript 复制代码
function move(animal: Animal) {
  if (animal instanceof Bird) {
    animal.fly();
  } else if (animal instanceof Fish) {
    animal.swim();
  } else {
    // do something...
  }
}

3. 处理未知类型

typescript 复制代码
function move(animal: unknown) {
  // do something...
}

三、写法

1. "instanceof " 操作符

 当你想要检查一个对象是否是某个类的实例时,可以使用instanceof

typescript 复制代码
abstract class Animal {
  abstract makeSound(): void;
}

abstract class Fish extends Animal {
  abstract swim(): void;
}

abstract class Bird extends Animal {
  abstract fly(): void;
}

class Shark extends Fish {
  makeSound() {
    console.log("Shark make sound");
  }

  swim() {
    console.log("Shark swim");
  }
}

class Eagle extends Bird {
  makeSound() {
    console.log("Eagle make sound");
  }

  fly() {
    console.log("Eagle fly");
  }
}

function getMovement(animal: Animal) {
  if (animal instanceof Fish) {
    // 在这个作用域(if创建的作用域)中,animal 被收窄为 Fish
    return animal.swim();
  }

  if (animal instanceof Bird) {
    // 在这个作用域(if创建的作用域)中,animal 被收窄为 Bird
    return animal.fly();
  }
}

2. "in" 操作符

 这种方式主要用于检查一个具有特定属性的对象。

typescript 复制代码
abstract class Animal {
  abstract makeSound(): void;
}

abstract class Fish extends Animal {
  abstract swim(): void;
}

abstract class Bird extends Animal {
  abstract fly(): void;
}

class Shark extends Fish {
  makeSound() {
    console.log("Shark make sound");
  }

  swim() {
    console.log("Shark swim");
  }
}

class Eagle extends Bird {
  makeSound() {
    console.log("Eagle make sound");
  }

  fly() {
    console.log("Eagle fly");
  }
}

function getMovement2(animal: Animal) {
  if ("swim" in animal) {
    return animal.swim();
  }

  if ("fly" in animal) {
    return animal.fly();
  }
}

3. 自定义类型守卫

 在复杂的应用中,可能需要基于特定逻辑来确定变量的类型,此时可以创建自定义的类型守卫。通过类型谓词"is" 实现变量类型的收窄。

typescript 复制代码
abstract class Animal {
  abstract makeSound(): void;
}

abstract class Fish extends Animal {
  abstract swim(): void;
}

abstract class Bird extends Animal {
  abstract fly(): void;
}

class Shark extends Fish {
  makeSound() {
    console.log("Shark make sound");
  }

  swim() {
    console.log("Shark swim");
  }
}

class Eagle extends Bird {
  makeSound() {
    console.log("Eagle make sound");
  }

  fly() {
    console.log("Eagle fly");
  }
}

function isAccurateAnimal<T extends Animal>(
  animal: unknown,
  type: abstract new (...args: any[]) => T
): animal is T {
  return animal instanceof type;
}

function getMovement(animal: Animal) {
  if (isAccurateAnimal(animal, Fish)) {
    animal.swim();
  }

  if (isAccurateAnimal(animal, Bird)) {
    animal.fly();
  }
}

 自定义类型守卫传入需要收窄类型的变量,并定义相应的收窄类型,返回布尔值的结果,当结果为真则变量的类型收窄为定义的类型,反之不进行收窄。

typescript 复制代码
function narrowType(object: unknown): object is [NarrowedType] {
  return [condition]
}
相关推荐
yume_sibai5 小时前
大屏数据可视化 - 边框红绿呼吸灯实现详解
前端·信息可视化·typescript
孟无岐15 小时前
LayaAir IDE 插件开发入门:用 TypeScript 给编辑器加一个自己的面板
typescript·游戏开发·工程化·编辑器扩展·layaair·ide 插件
shakingWaves16 小时前
better auth worker on cloudflare
typescript·cloudflare·auth·better-auth
退休倒计时1 天前
【每日一题】LeetCode 437. 路径总和 III TypeScript
算法·leetcode·typescript
柯克七七1 天前
给老项目加了 TypeScript,本来只想自己爽,结果全公司代码审查标准被我抬高了
前端·vue.js·typescript
yume_sibai2 天前
高德地图骑行导航 API 介绍与使用方法
前端·typescript
子兮曰2 天前
TypeScript 7.0 RC 深度解读:Go 重写完成,10 倍提速,编译器的「奇点时刻」
前端·后端·typescript
BAIGAOa2 天前
不止 React:我开源了一个框架无关的终端键盘引擎,Vue/Svelte 都能用
typescript
小蚂蚁i2 天前
把 TypeScript 内置工具类型彻底搞懂,顺便自己封几个好用的
前端·typescript
爸爸6192 天前
07 ArkTS 基础类型与接口定义:从 TypeScript 到鸿蒙的类型升级
javascript·华为·typescript·harmonyos·鸿蒙系统