microsoft / microsoft/TypeScript

Narrowing empty string or false in generic misbehaves

未关闭
#58,232 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Domain: check: Control Flow Help Wanted Possible Improvement
主要语言
Go
星标
111k
派生
14.4k
平均合并
1 天 19 小时
30 天内合并 PR
117

描述

🔎 Search Terms

generic, narrowing, empty string, false, falsy

🕗 Version & Regression Information

This is the behavior in every version I tried, but it seems to behave a little worse with TS v4.2.3 and prior.

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.5.0-dev.20240417#code/GYVwdgxgLglg9mABKSBGAPAFUQUwB5Q5gAmAzomCALYBGOATogD4UgA2bzi4xOwMYHMS7AAhm1I4uAcmkA+ABR4AXK1oMumAJSIA3gCgAkPRxQQ9JHmYsefAUIDc+gL76U0eEncAmLLgJEZGp0jCyUHFy2-ILCLGISUiyyiirBGizaekYmZhaIVgD8+YhyiAAMiEVWqgC01flOru6wCMjgEADMfviEJOSU6qGsETYkdjEi4pIy8kqqAyGaOgbGpuaWlYgAsqJQABYAdKI0pEo6qniN+voQCKRQ+fPUi6O80UKIALxtaAqyWk5bmB7ogAJ5fH4QbwKGodAE3O4PABeEPcHRhcIcQA

💻 Code
function func1<T extends number | null | undefined | false | ''>(x: number | T) {
	return x || undefined;
}
function func2<T extends number | null | undefined | false | ''>(x: number | T) {
	return x ? x > 0 ? x : -x : x;
}
function func3<T extends number | null | undefined | false | ''>(x: number | T) {
	return x ? Math.abs(x) : x;
}

const x: number | undefined = func1('');
const y = func2(-3);
const z = func3(-3);
🙁 Actual behavior

The inferred return type of func1 can contain false or '' depending on T. It's wrong because x || undefined can never result in false or '' and thus should not be in the resulting type.

func2 fails type checking: Operator '>' cannot be applied to types 'number | NonNullable<T>' and 'number'.. It's wrong because the truthiness check should narrow x to number. (Note that func3 succeeds type checking because the signature of Math.abs seems to provide supplemental information which narrows x to number.)

🙂 Expected behavior

The inferred return type of func1 is should number | undefined, regardless of T.

func2 is expected to pass type checking, x having been narrowed to number with the truthiness check.

Additional information about the issue

All functions act 'correctly' if defined without the use of generics, but it's inadequate because the return type of func2 and func3 would always be number | null | undefined | false | '' regardless of argument type.

My actual use case is creating functions that transform numeric inputs and propagate falsy values. I've worked around this issue using type assertions in spots where narrowing misbehaves.

贡献指南

打开贡献指南

从这里开始

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

调研方向

从链接的 TypeScript Playground 示例开始,并使用报告中的编译器版本在 func1、func2 和 func3 中复现该行为。跟踪 truthiness 检查下泛型参数的类型检查和控制流收窄;当 func1 排除 false 和 '',而 func2 在 x 收窄为 number 的情况下通过类型检查时,即表示完成。

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

评估

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

把新 issue 发到你的邮箱

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