microsoft / microsoft/TypeScript

Allow explicit fallthrough when noFallthroughCasesInSwitch is enabled

未关闭
#62,295 1 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

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

描述

### 🔍 Search Terms

fallthrough noFallthroughCasesInSwitch

### ✅ Viability Checklist

- [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
- [x] This wouldn't change the runtime behavior of existing JavaScript code
- [x] This could be implemented without emitting different JS based on the types of the expressions
- [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- [x] This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- [x] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

### ⭐ Suggestion

```typescript
switch (...) {
case 'A':
doSomething();
// fallthrough - this will allow falling to next statement
case 'B':
doSomethingElse();
}
```

Instead of usage of `@ts-expect-error` this is nicely readable and intention is clearly visible in generated code.

### 📃 Motivating Example

Until now for `noFallthroughCasesInSwitch` is missing good way to allow falling through if needed. The only possible way `@ts-expect-error` is disabling not only fallthrough check, but also other checks which is not desired.

### 💻 Use Cases

1. What do you want to use this for? Because eslint rule no-fallthrough does not handle TS exhaustive match, I would like to you `noFallthroughCasesInSwitch` config rule
2. What shortcomings exist with current approaches?
- eslint can not be used, its `no-fallthrough` rule can not detect exhaustive match, so the following code is incorrectly reported by eslint:
```typescript
function transform(action: 'KEEP' | 'INVERSE', b: boolean): boolean {
switch (action) {
case 'KEEP':
switch (b) {
case true:
return true;
case false:
return false;
}
case 'INVERSE':
switch (b) {
case true:
return false;
case false:
return true;
}
}
}
```

- satisfying eslint is not possible e.g. by putting `break`, because TS will start report _Unreachable code detected_ and this can not be disabled by `@ts-expect-error`
- disabling reported case by `@ts-expect-error` is also not viable, because it is disabling all type checks, so e.g. in case `case something:` validation that `something` has the correct type is also disabled (and issue #19139 is still opened)
4. What workarounds are you using in the meantime? In my specific case, I had only one of these switches with exhaustive match, so I could put the statement as the last to not disable any check.

Implementation of #19139 would give me decent workaround and it is definitely more useful that this specific feature, but I believe both of them should be implemented.

贡献指南

打开贡献指南

从这里开始

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

调研方向

该 issue 没有指定文件或测试;先定位 TypeScript 编译器中的 noFallthroughCasesInSwitch 诊断和 switch 控制流检查。将提议的 fallthrough 标记与现有的 @ts-expect-error 和 exhaustive-match 用例进行比较,然后添加针对性的覆盖,表明有意的 fallthrough 仍会进行类型检查。

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

评估

技术栈
typescript
领域
compilers
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

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