Skip to content

Type-checked loop conditions #51036

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
5 tasks done
Rudxain opened this issue Oct 3, 2022 · 2 comments
Closed
5 tasks done

Type-checked loop conditions #51036

Rudxain opened this issue Oct 3, 2022 · 2 comments

Comments

@Rudxain
Copy link

Rudxain commented Oct 3, 2022

Suggestion

There should be a tsconfig option to enable strict type-checking of loop condition predicates

🔍 Search Terms

while, loop, for, bool, boolean

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • 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

A tsconfig bool option named typeCheckedLoops (or something else) that throws an error when any condition isn't a boolean

📃 Motivating Example

I was surprised that TS allowed the following (even with very strict config):

while ("true");

while (!"");

while ([]);

for (;{};);

None of those are bools (except !""), yet they don't trigger any errors

💻 Use Cases

This becomes a problem when the condition is a variable, or an expression composed of multiple vars.

@MartinJohns
Copy link
Contributor

Essentially a duplicate of #38826.

@Rudxain
Copy link
Author

Rudxain commented Oct 3, 2022

Thank you for the info. I'll close this

@Rudxain Rudxain closed this as not planned Won't fix, can't repro, duplicate, stale Oct 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants