microsoft / microsoft/TypeScript

Add option to throw error when `!` (NonNull expression) is used on a non-nullish expression

Abierto
#58,072 3 comentarios 8 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Awaiting More Feedback Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

🔍 Search Terms

"cast", "non null", "nonnull", "@ts-expect-error", "strict", "error"

✅ Viability Checklist
⭐ Suggestion

Add in the TSConfig.json a way to tell TS to check when devs are using ! (non null expressions) on variables that are non null

📃 Motivating Example

Mimic @ts-ignore vs @ts-expect-error but for !:
In well maintained codebases, @ts-expect-error can be preferred to @ts-ignore to get rid of those as soon as they become non necessary.
A similar thinking could be applied to ! so that we can keep healthy codebases

💻 Use Cases
  1. What do you want to use this for?

Seeing ! in a codebase should mean that the variable can be nullish, and that we're telling TS that it isn't in practice.
It replicates @ts-expect-error but for !, at one difference: when using @ts-expect-error you can pick if you want this or @ts-ignore. With this proposal, it'd be set in the TSConfig so would get applied to all ! (but this is I think what devs would want)

  1. What shortcomings exist with current approaches?

In large codebases, we can't keep track of why a ! was used, and whether it's still required. So we need to come back to them, and remove them manually to see if TS is happy or not without them. We can use ESLint to warn us against the use of ! but it's a 100% process.

  1. What workarounds are you using in the meantime?

TypeScript-ESLint can be used to either warn / ban ! and instead push devs to use ?. or ??, but sometimes a variable is indeed never nullish, so it modifies / slows down the runtime for no real reason.

As TypeScript-ESLint can also have access to the full TS process, we could also use it to check the type of the variable on which ! is used to see if it's necessary. But this requires spawning the full TS process in ESLint, slowing it down significantly on large codebases (which is why having it directly in TS could be better)

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

Revisa la distinción de la propuesta entre aserciones non-null necesarias y redundantes, junto con la comparación con @ts-expect-error y la opción de TSConfig solicitada. Establece cómo debe informar la opción sobre ! en expresiones que ya se sabe que no son nullish y define pruebas para las configuraciones habilitada y deshabilitada antes de la implementación.

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

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.