microsoft / microsoft/TypeScript

Typescript should prevent invalid variable assigning in if conditions

Open
#45,542 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: check: Control Flow In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no implementation files, tests, or entry points. Start by reviewing the TypeScript checker’s handling of assignments in if conditions and the reported examples, including the valid while-loop case. Done should define diagnostic behavior that distinguishes accidental assignments from valid assignment-and-comparison patterns, with tests covering those cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.