「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]
}
相关推荐
甜兒.4 小时前
鸿蒙小技巧
前端·华为·typescript·harmonyos
一介俗子14 小时前
TypeScript 中 extends 关键字
typescript
mez_Blog15 小时前
个人小结(2.0)
前端·javascript·vue.js·学习·typescript
QGC二次开发18 小时前
Vue3 : Pinia的性质与作用
前端·javascript·vue.js·typescript·前端框架·vue
2301_8010741520 小时前
TypeScript异常处理
前端·javascript·typescript
下雪天的夏风1 天前
TS - tsconfig.json 和 tsconfig.node.json 的关系,如何在TS 中使用 JS 不报错
前端·javascript·typescript
天下无贼!2 天前
2024年最新版TypeScript学习笔记——泛型、接口、枚举、自定义类型等知识点
前端·javascript·vue.js·笔记·学习·typescript·html
Jorah4 天前
1. TypeScript基本语法
javascript·ubuntu·typescript
小白小白从不日白4 天前
TS axios封装
前端·typescript
aimmon5 天前
Superset二次开发之源码DependencyList.tsx 分析
前端·typescript·二次开发·bi·superset