Repository: TypeScript
Issue: Typescript should prevent invalid variable assigning in if conditions
Labels: Suggestion,In Discussion,Domain: Control Flow

# Suggestion

<!--
  Please fill in each section completely. Thank you!
-->

## 🔍 Search Terms

<!--
  💡 Did you know? TypeScript has over 2,000 open suggestions!
  🔎 Please search thoroughly before logging new feature requests as most common ideas already have a proposal in progress.
  The "Common Feature Requests" section of the FAQ lists many popular requests: https://github.com/Microsoft/TypeScript/wiki/FAQ#common-feature-requests

  Replace the text below:
-->
"assigning if", I don't know what else to look for

## ✅ Viability Checklist

<!--
   Suggestions that don't meet all these criteria are very, very unlikely to be accepted.
   We always recommend reviewing the TypeScript design goals before investing time writing
   a proposal for ideas outside the scope of the project.
-->
My suggestion meets these guidelines:

* [X] This wouldn't be a breaking change in existing TypeScript/JavaScript code (would only detect errors that are already occurring)
* [X] This wouldn't change the runtime behavior of existing JavaScript code
* [X] This could be implemented without emitting different JS based on the types of the expressions
* [X] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
* [X] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).


## ⭐ Suggestion

<!-- A summary of what you'd like to see added or changed -->
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
```ts
let variable = "text"
if (variable = "value") // rest of the code
```
When they actually meant to do this
```ts
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:
```js
	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

<!--
  If you were announcing this feature in a blog post, what's a short explanation that shows
  a developer why this feature improves the language?
-->
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

<!--
  What do you want to use this for?
  What shortcomings exist with current approaches?
  What workarounds are you using in the meantime?
-->
Basically, whenever you're coding while tired and accidentally write this, TS would alert you like all other TS features.