microsoft / microsoft/TypeScript

NaN incorrectly narrows to 0

未关闭
#36,964 4 条评论 4 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Awaiting More Feedback Suggestion
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

TypeScript Version: 3.9.0-dev.20200222

Search Terms: NaN Truthiness Falsy

Code

function bool(): boolean { return false; }
function number(): number { return NaN; }

let a = number();
let b = bool();

const x = a && b; // type = boolean | 0
console.log(x); // prints NaN (which is neither boolean, nor 0)

Expected behavior:
Type of x should be number | boolean.

Since NaN isn't a "unit type" like 0, we can't express NaN without using number.

Actual behavior:
Type of x is 0 | boolean.

Playground Link: playground link

Related Issues:
https://github.com/microsoft/TypeScript/issues/32778 (Incorrectly marked as duplicate?)

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

在链接的 TypeScript Playground 中重现该示例,并将 a && b 的推断类型与报告的运行时值进行比较。阅读相关 issue #32778,并跟踪编译器对 number 值进行的 truthiness narrowing;当 NaN 不再被缩小为 0,且预期类型 number | boolean 有回归测试覆盖时,即视为完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
compilers
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。