microsoft / microsoft/TypeScript
`asserts` keyword does not enhance hoisted function type, works on functions assigned to a variable
未关闭
还没有人认领这个 Issue。
Bug
Domain: check: Control Flow
Help Wanted
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
TypeScript Version: 4.0.2, 4.1.0-beta
Search Terms:
asserts function
Code
interface LabelledFunction { label: string; }
function assignLabel(fn: () => unknown, label: string): asserts fn is LabelledFunction {
Object.assign(fn, { label });
}
function a() { }
assignLabel(a, 'a');
console.info(a.label); // Property 'label' does not exist on type '() => void'.(2339)
const b = function () { };
assignLabel(b, 'b');
console.info(b.label); // works as expected
const c = () => { };
assignLabel(c, 'c');
console.info(c.label); // works as expected
Expected behavior:
At least in runtime code coming after assignLabel(a, 'a');, a.label should be accessible.
Actual behavior:
Property 'label' does not exist on type '() => void'.(2339)
Playground Link: link
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从链接的 TypeScript Playground 复现开始,并在调用 assignLabel 后,将 hoisted 函数 a 与赋值给变量的函数 b 和 c 进行比较。跟踪 type-checker 对 assertion function 的行为,并确认在调用后的代码中接受 a.label,且不会使正常工作的案例出现回归后即可完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 冷清
- 描述清晰度
- 描述清楚
- 新手友好度
- 48/100