microsoft / microsoft/TypeScript

Typescript should prevent invalid variable assigning in if conditions

Offen
#45,542 5 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Domain: check: Control Flow In Discussion Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Das Issue nennt keine Implementierungsdateien, Tests oder Einstiegspunkte. Beginne mit der Überprüfung, wie der TypeScript-Checker Zuweisungen in if-Bedingungen und die gemeldeten Beispiele behandelt, einschließlich des gültigen while-Schleifenfalls. Done sollte ein Diagnoseverhalten definieren, das versehentliche Zuweisungen von gültigen Zuweisungs-und-Vergleichsmustern unterscheidet, mit Tests, die diese Fälle abdecken.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.