Create flag strictVariableInitialization
还没有人认领这个 Issue。
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 30/100
- Issue 类型
- 功能
- 描述清晰度
- 描述清楚
- 活跃度
- 停滞
- 技术栈
- javascript, typescript
- 领域
- compilers
调研方向
首先审查拟议的 strictVariableInitialization 行为及其提出动机的 Closure 示例,然后将其与 strictPropertyInitialization 进行比较。定义应如何处理可达代码路径和包含 undefined 的类型;当该提案的设计已经确定,并且编译器具有相应行为且不改变生成的 JavaScript 时,即视为完成。
由索引模型根据 Issue 内容生成。
描述
🔍 Search Terms
uninitialized variable, undefined, closure, strict property initialization,
✅ 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
I would like to see a check that ensures that no variable whose type is not permitted to be undefined may remain uninitialized at the end of its scope.
📃 Motivating Example
TypeScript allows unsafety that can and does catch users by surprise when using variables in a closure. Normally, TS won't let you access an uninitialized variable:
function doSomething() {
let foo: string;
foo.toLowerCase(); // TS ERROR: Variable 'foo' is used before being assigned
}
However, TypeScript optimistically assumes that variables are initialized when used in closures.
let foo: string;
function printFoo() {
console.log(foo.toLowerCase());
}
printFoo(); // Uncaught TypeError: Cannot read properties of undefined (reading 'toLowerCase')
That's for good reason, but sometimes, as in the above case, this is provably unsafe, since foo is guaranteed not to be initialized.
The new flag "strictVariableInitialization" ensures that a variable must be initialized by the end of all reachable codepaths in its scope.
let foo: string; // (proposed) TS ERROR: `foo` is not initialized in all reachable codepaths
function printFoo() {
console.log(foo.toLowerCase());
}
printFoo()
Of course, variables whose type includes undefined are still permitted to be uninitialized.
let foo: string | undefined;
function printFoo() {
console.log(foo?.toLowerCase());
}
printFoo();
This check is highly analogous to strictPropertyInitialization for classes.
💻 Use Cases
See https://github.com/typescript-eslint/typescript-eslint/issues/9565 for a somewhat-wordier proposal in typescript-eslint, and https://github.com/typescript-eslint/typescript-eslint/issues/4513 and https://github.com/typescript-eslint/typescript-eslint/issues/10055#issuecomment-2374797860 for cases where this has caused confusion in the wild.
In short, people who have written code that does check for initialization of non-nullable variables become confused by the linter informing them that the check is unnecessary according to the types, even though they can see that it is necessary at runtime:
let foo: Something
function useFoo() {
if (foo != null) { // linter flags this condition as unnecessary since foo cannot be nullish
foo.bar();
}
}
The code should be rewritten as
let foo: Something | undefined
function useFoo() {
if (foo != null) {
foo.bar();
}
}
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.4k
- 平均合并
- 1 天 15 小时
- 30 天内合并 PR
- 106
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
microsoft/TypeScript 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 88/100
microsoft/TypeScript#64322 · 2 条评论 · 1 个 reaction · 已指派 2 人 ·
-
Possible Improvement
难度 2/5 1-3 小时 新手友好度 78/100
microsoft/TypeScript#64278 · 1 条评论 · 1 个 reaction ·
-
Docs
难度 2/5 1-3 小时 新手友好度 70/100
microsoft/TypeScript#64118 · 1 条评论 ·
-
难度 1/5 1 小时以内 新手友好度 88/100
microsoft/TypeScript#64094 ·
-
Docs
难度 2/5 1-3 小时 新手友好度 76/100
microsoft/TypeScript#63959 · 5 条评论 ·
查看 microsoft/TypeScript 的全部 Issue
相似的 Issue
-
Type/Bug
难度 2/5 1-3 小时 新手友好度 78/100
OpenNSW/nsw-srilanka#497 ·
-
难度 1/5 1 小时以内 新手友好度 92/100
milvus-io/birdwatcher#545 ·
-
kind/bug
难度 2/5 1-3 小时 新手友好度 88/100
kubernetes-sigs/prow#953 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 88/100
-
难度 2/5 1-3 小时 新手友好度 88/100
caddyserver/caddy#8046 ·