microsoft / microsoft/TypeScript

Allow explicit fallthrough when noFallthroughCasesInSwitch is enabled

Abierto
#62,295 1 comentario 3 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Awaiting More Feedback Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
2 d 4 h
PR fusionados (30 d)
132

Descripción

🔍 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.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

El issue no menciona archivos ni tests; empieza por localizar el diagnóstico noFallthroughCasesInSwitch y las comprobaciones de control de flujo de switch en el compilador de TypeScript. Compara el marcador de fallthrough propuesto con los casos existentes de @ts-expect-error y exhaustive-match, y añade cobertura específica que muestre que el fallthrough intencional sigue sometiéndose a comprobación de tipos.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
compilers
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.