microsoft / microsoft/TypeScript

Allow explicit fallthrough when noFallthroughCasesInSwitch is enabled

Ouverte
#62,295 1 commentaire 3 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Awaiting More Feedback Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

🔍 Search Terms

fallthrough noFallthroughCasesInSwitch

✅ Viability Checklist
⭐ Suggestion
    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:
    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)
  3. 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.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

L’issue ne nomme aucun fichier ni test ; commencez par localiser le diagnostic noFallthroughCasesInSwitch et les vérifications du contrôle de flux de switch dans le compilateur TypeScript. Comparez le marqueur de fallthrough proposé aux cas existants de @ts-expect-error et exhaustive-match, puis ajoutez une couverture ciblée montrant qu’un fallthrough intentionnel continue d’être vérifié par le système de types.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.