microsoft / microsoft/TypeScript
Allow type narrowing to be specified as always-on or always-off
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.4k
- 平均合并
- 1 天 19 小时
- 30 天内合并 PR
- 117
描述
🔍 Search Terms
type narrowing of functions, asserts on getters
✅ Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
Right now, properties of objects can always be narrowed, whereas functions can never be narrowed. As a developer, I would like to specify when functions can be narrowed, and when object properties can't be narrowed.
📃 Motivating Example
Consider this example:
export const model = {
get value() {
return Math.random() > 0.5 ? 'Hello' : undefined
},
getValue() {
return model.value
}
}
if (model.value) {
console.log(model.value.toLowerCase())
}
if (model.getValue()) {
console.log(model.getValue().toLowerCase())
}
In the TypeScript playground, you can see that even though these code paths are identical in terms of output and function calls, one is narrowed and one is not. TypeScript always assumes that properties are always stable in their values between calls, and assumes that functions are always in-stable in their calls.
You can see this in the TypeScript error which does not error for the property, even though it should.
💻 Use Cases
- What do you want to use this for?
I'm more concerned with the stable function case than the instable property object case. I'm building a Knockout-like observable library that lays on top of Vue. It works fine / perfectly in the Vue ecosystem, however it's TypeScript that doesn't behave here. foo.value is always type-narrowed, whereas foo() is never type-narrowed, even though I can guarantee its stability between calls. Because this behavior in TypeScript is automatic (as far as I know?), there's no way to specify which function calls are stable (and can be narrowed) and which ones are not.
- What shortcomings exist with current approaches?
You can type narrow by assigning the returned value of the function to another variable. However, in Vue, this approach is limited when binding to templates, where v-if will not type-narrow a functional getter.
- What workarounds are you using in the meantime?
A very clumsy workaround is using a Vue computed(), which then also type-narrows.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 TypeScript playground 示例开始,复现 model.value 和 model.getValue() 的不同 narrowing 行为。阅读现有的类型 narrowing 行为,然后为稳定的函数调用和对象属性定义并验证一种 opt-in 或 opt-out 机制;完成的标准是所请求的情况能够一致地进行 narrowing,同时不改变 JavaScript 运行时输出。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- javascript, typescript
- 领域
- compilers
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 28/100