microsoft / microsoft/TypeScript
Allow explicit fallthrough when noFallthroughCasesInSwitch is enabled
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
### 🔍 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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Das Issue nennt keine Dateien oder Tests; beginne damit, die Diagnose noFallthroughCasesInSwitch und die Switch-Kontrollflussprüfungen im TypeScript-Compiler zu lokalisieren. Vergleiche den vorgeschlagenen Fallthrough-Marker mit den bestehenden Fällen für @ts-expect-error und exhaustive-match und füge gezielte Abdeckung hinzu, die zeigt, dass beabsichtigter Fallthrough weiterhin typgeprüft wird.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100