microsoft / microsoft/TypeScript

strictNullCheck should detect unreachable default values and code after nullish operator

Abierto
#43,040 3 comentarios 2 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
1 d 19 h
PR fusionados (30 d)
117

Descripción

Suggestion

Using strictNullCheck should allow detection of code that isn't supposed to be reached according to type analysis

🔍 Search Terms

strictNullCheck unreachble nullish default value

✅ Viability Checklist

My suggestion meets these guidelines:

  • 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 feature would agree with the rest of TypeScript's Design Goals.

Yes, especially "Statically identify constructs that are likely to be errors."

⭐ Suggestion

This assumes that it is desired to detect errors with unreachable code due to type analysis (see microsoft/TypeScript-Website#1604 ).

With strictNullCheck turned on, some code that is meant to be unreachable and thus an error is not flagged.

📃 Motivating Example

The = 42 below is unreachable (playground) and is the sign that some code is likely to be an error, but nothing is flagged

class Example {
  foo({
    x = 42
  }: {
    x: number
  }) {
    return x
  }
}

Either the declaration should be changed to x?: number, or the default should be removed.

Another example (thanks to @jcalz) using the nullish operator.

function foo(x: number) { x ??= 42 }

Again, it would be helpful if an error or a warning would be raised as that code seems clearly to be a mistake.

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

Comienza reproduciendo los dos ejemplos motivadores con strictNullCheck habilitado: el valor predeterminado x = 42 desestructurado y x ??= 42. Determina los diagnósticos previstos para los valores predeterminados inalcanzables y las asignaciones nullish; después, usa la suite de pruebas existente del compilador de TypeScript para verificar ambos casos y su comportamiento en el estado completado.

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
32/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.