Skip to content

in 运算符作用是什么?

Posted on:2024年9月5日 at 01:22

在 TypeScript 中,in 运算符用于检查一个对象是否具有特定的属性。它的基本语法是:

property in object;

用途

  1. 检查对象是否包含某个属性

    in 运算符用于检查某个对象是否拥有指定的属性。它会返回一个布尔值,表示属性是否存在。

    示例

    const person = { name: "Alice", age: 30 };
    
    console.log("name" in person); // true
    console.log("gender" in person); // false
    

    在这个示例中,"name" in person 返回 true,因为 person 对象具有 name 属性。"gender" in person 返回 false,因为 person 对象没有 gender 属性。

  2. 类型保护

    in 运算符也可以用作类型保护,用于在条件判断中检查对象是否具有特定的属性,从而确定对象的具体类型。

    示例

    type Cat = { type: "cat"; meow: () => void };
    type Dog = { type: "dog"; bark: () => void };
    
    function handleAnimal(animal: Cat | Dog) {
      if ("meow" in animal) {
        animal.meow(); // 类型保护确保 animal 是 Cat
      } else {
        animal.bark(); // 类型保护确保 animal 是 Dog
      }
    }
    

    在这个示例中,通过检查 animal 对象是否具有 meow 属性来判断它是 Cat 还是 Dog"meow" in animal"bark" in animal 用于确定对象的具体类型,并执行相应的方法。

原文转自:https://fe.ecool.fun/topic/bbe72a8a-02b6-43bc-96ce-d3d98109cd0b