微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

键入复合类型保护 - 从类型保护推断/继承属性到其调用者

如何解决键入复合类型保护 - 从类型保护推断/继承属性到其调用者

type Bird = { fly: () => "fly" };
type Insect = { annoy: () => "annoy" };
type Dog = { beTheBest: () => "dogdogdog" };

type Animal = Bird | Insect | Dog;

const isBird = (animal: Animal): animal is Bird => {
  if ("fly" in animal) return true;
  return false;
};

const isInsect = (animal: Animal): animal is Insect => {
  if ("annoy" in animal) return true;
  return false;
};

const hasWings = (animal: Animal) => {
  if (isBird(animal)) return true;
  if (isInsect(animal)) return true;
  return false;
};

我将两个基本的类型保护函数组合到复合 hasWings 保护中,但 TypeScript 不会推断它的类型保护特性 - 它只是将 typeof hasWings 推断为 (a: Animal) => boolean。有没有一种方法可以帮助 TS 推断或明确指出 hasWingsisInsectisBird 类型保护的组合,而无需手动重新指定标准?

// Something like this would be useful to me:
const hasWings = (animal: Animal): ReturnType<typeof isBird> & ReturnType<typeof isInsect> => {
  if (isBird(animal)) return true;
  if (isInsect(animal)) return true;
  return false;
};

// Having to specify the whole list manually is not useful to me:
const hasWings = (animal: Animal): animal is Insect | Bird => {
  if (isBird(animal)) return true;
  if (isInsect(animal)) return true;
  return false;
};

解决方法

您可以编写一个辅助函数来以这种方式组合类型保护。类型注释是一口,但它的工作原理:

type TypeGuard<S,T extends S> = (x: S) => x is T;

function typeGuardUnion<T extends TypeGuard<any,any>[]>(...fs: T): TypeGuard<
    T extends TypeGuard<infer S,any>[] ? S : never,T extends TypeGuard<any,infer U>[] ? U : never
> {
    return ((x: any) => fs.some(f => f(x))) as any;
}

// TypeGuard<Animal,Bird | Insect>
const hasWings = typeGuardUnion(isBird,isInsect);

Playground Link

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?