question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript should prevent invalid variable assigning in if conditions

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
andrewbranchcommented, Aug 23, 2021

Seems like a reasonable candidate for This condition will always return true if the RHS is an always-truthy type, or at least a truthy literal… I don’t think we’d ban this pattern outright (that feels solidly in the domain of a linter), but obviously-truthy conditions look beyond suspicious.

1reaction
ImRodrycommented, Aug 23, 2021

Related: #38826

Although I can see how that suggestion can relate to this one, it goes one step forward in enforcing a boolean on every condition, which wouldn’t be ideal at least to me. What I’m talking about here is producing an error on something that is actually invalid, so I believe this should be checked for without any additional settings

Read more comments on GitHub >

github_iconTop Results From Across the Web

Assign variable in if condition statement, good practice or not?
Assignment in a conditional statement is valid in javascript, because your just asking "if assignment is valid, do something which possibly includes the...
Read more >
Documentation - Variable Declaration - TypeScript
When a variable is declared using let , it uses what some call lexical-scoping or block-scoping. Unlike variables declared with var whose scopes...
Read more >
Documentation - Conditional Types - TypeScript
Create types which act like if statements in the type system.
Read more >
Documentation - Narrowing - TypeScript
In JavaScript, we can use any expression in conditionals, && s, || s, if statements, Boolean negations ( ! ), and more. As...
Read more >
Documentation - TypeScript 2.0
In strict null checking mode the compiler requires every reference to a local variable of a type that doesn't include undefined to be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found