TypeScript 可以通过“提取"推断出可区分联合的类型吗?布尔逻辑? [英] Can TypeScript infer type of a discriminated union via "extracted" boolean logic?

查看:22
本文介绍了TypeScript 可以通过“提取"推断出可区分联合的类型吗?布尔逻辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更频繁地使用歧视工会 (DU) 并开始喜欢它们.然而,我确实有一个我似乎无法解决的问题.如果您内联 DU 的布尔检查,您可以依靠 TypeScript (TS) 自动为您推断类型.但是,如果您提取布尔检查,TS 就不能再缩小到 DU 的特定子类型.我知道类型保护,但我想知道为什么编译器不支持提取的在线检查,特别是.

I have been using discriminated unions (DU) more frequently and have come to love them. I do however have one issue I can't seem to get past. If you inline a boolean check for the DU, you can rely on TypeScript (TS) to automatically infer the type for you. However, if you extract the boolean check, TS can no longer narrow to the specific subtype of the DU. I’m aware of type guards, but I’d like to know why the compiler doesn’t support extracted online checks, specifically.

这是一个已知的限制吗?我应该提交错误/功能请求吗?

Is this a known limitation? Should I file a bug/feature request?

实施例这里(W/打字稿游乐场链接):

type A = { type: "A"; foo: number };
type B = { type: "B"; bar: string };
type DU = A | B;

const checkIt = (it: DU) => {
  const extractedCheck = it.type === "A";

  if (extractedCheck) {
    // it does not get narrowed
    console.log(it.foo); // Error: Property 'foo' does not exist on type 'DU'.
  }

  if (it.type === "A") {
    // but this is fine
    console.log(it.foo);
  }
};

推荐答案

针对 TS4.4 的更新:

TypeScript 4.4 将支持将类型保护的结果保存到 const,如 microsoft/TypeScript#44730.此时,您的代码示例将正常工作:

UPDATE FOR TS4.4:

TypeScript 4.4 will introduce support for saving the results of type guards to a const, as implemented in microsoft/TypeScript#44730. At this point, your code example will just work:

const checkIt = (it: DU) => {
  const extractedCheck = it.type === "A";

  if (extractedCheck) {
    console.log(it.foo); // okay
  }

};

游乐场链接到代码

microsoft/TypeScript#12184 上有一个现有的功能请求,以允许此类类型保护结果要保存"转换为稍后使用的命名值.该请求是开放的,但被列为重新访问"因为没有明显的方法可以以高性能的方式实现它.语言首席架构师的话是:

There is an existing feature request at microsoft/TypeScript#12184 to allow such type guard results to be "saved" into a named value to be used later. The request is open but listed as "revisit" because there's no obvious way to implement it in a performant way. The word from the lead architect of the language is:

这将要求我们跟踪一个变量的特定值对其他变量意味着什么影响,这会给控制流分析器增加大量复杂性(以及相关的性能损失).不过,我们会保留它作为建议.

This would require us to track what effects a particular value for one variable implies for other variables, which would add a good deal of complexity (and associated performance penalty) to the control flow analyzer. Still, we'll keep it as a suggestion.

因此,不幸的是,我不希望很快会在该语言中看到这样的功能.

So I wouldn't expect to see such a feature in the language anytime soon, unfortunately.

我的建议是继续使用您的内联类型检查.如果您有更复杂的类型保护情况,则可能值得制作 用户定义的类型保护函数,但我不认为这是对示例代码中案例的改进.

My suggestion is just to continue using your inline type check. If you have a more complex type guard situation it may be worthwhile to make a user-defined type guard function but I don't see that as an improvement for the case in your example code.

好的,希望有帮助;祝你好运!

Okay, hope that helps; good luck!

这篇关于TypeScript 可以通过“提取"推断出可区分联合的类型吗?布尔逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆