microsoft / microsoft/TypeScript

Typescript should prevent invalid variable assigning in if conditions

Abierto
#45,542 5 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Domain: check: Control Flow In Discussion Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

Suggestion

🔍 Search Terms

"assigning if", I don't know what else to look for

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code (would only detect errors that are already occurring)
  • 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.

⭐ Suggestion

I believe a lot of JavaScript programmers have accidentally written code that assigns a new value to a variable instead of comparing it to something else by doing the following

let variable = "text"
if (variable = "value") // rest of the code

When they actually meant to do this

let variable = "text"
if (variable === "value") // rest of the code

And this code is invalid and an error should be thrown by TypeScript before runtime because it is essentially creating an invalid if condition that will never run. A possible problem that could come from creating an error in this would be situations where variables are correctly reassigned and then compared to something, like in the following example:

	let matches;
    while ((matches = this.constructor.CHANNELS_PATTERN.exec(this._content)) !== null) {
      const chan = this.client.channels.cache.get(matches[1]);
      if (chan) this._channels.set(chan.id, chan);
    }

Even though the above example uses a while, it's still a valid use case where an error should not be thrown since it is being compared later.

📃 Motivating Example

TypeScript will now be able to detect invalid if conditions and help you improve them before runtime so that they aren't ignored.

💻 Use Cases

Basically, whenever you're coding while tired and accidentally write this, TS would alert you like all other TS features.

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 de implementación, pruebas ni puntos de entrada. Empieza revisando cómo el checker de TypeScript gestiona las asignaciones en condiciones if y los ejemplos reportados, incluido el caso válido de un bucle while. Done debería definir un comportamiento de diagnóstico que distinga las asignaciones accidentales de los patrones válidos de asignación y comparación, con pruebas que cubran esos casos.

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

Evaluación

Stack tecnológico
javascript, 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.