-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New compiler option to disallow for...in
/in
for types that do not have any properties
#53155
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
Comments
Are you talking about the |
I feel like this is more in linter's land. There's also typescript-eslint/typescript-eslint#5677 for the |
@RyanCavanaugh I am treating them as roughly equivalent.
I did also consider that ... however this is a type checking issue and typescript linters afaik do not check types, so I think this does belong here.
It's not just about Sidenote: There also is the guard-for-in eslint lint ... however it is very lax since it checks for the presence of any if block (so e.g. |
Also, do note that the |
in
operator for types that do not have any propertiesfor...in
/in
for types that do not have any properties
Thanks for pointing that out! I just found the more thorough explanation at Linting with Type Information. I guess the bigger question is whether or not more strict type checking should be part of the TypeScript compiler vs a separate linter plugin. TypeScript already has 18 specific such rules: Looking at the typescript eslint rules that require type information I think e.g. no-unsafe-call or no-misused-promises would also fit quite well into the builtin settings (as would the setting I propose here in my opinion), because such strict checks help achieve the first design goal of TypeScript:
|
|
Personally I would like TypeScript to provide strict type safety by default. I think there is a clear difference between opinionated lints and type checking (that is flagging types that are used in a way they're not supposed to be used). However since this is a bigger question about the general scope of TypeScript, I am closing this much more specific issue since as you rightfully pointed out this can already be implemented in typescript-eslint. Thanks for pointing this out and being so responsive! |
I don't consider using That said, |
@fatcerberus The request forbids them on iterables, because the idea is that they are usually ordered collections rather than keyed collections. |
@Josh-Cena I was going by the issue title which says “…for types that do not have any properties”. |
I feel like as far as rules go, saying that The standalone |
The idea is to introduce a new compiler option, e.g.
strictInOperator
which when enabled would flag for example the following for loop as a type error:because
Array.prototype.entries
returns anIterableIterator
that does not have any properties so the above for loop would never iterate (you'd have to useof
instead ofif
), which is a very easy mistake to make especially when you're used to other programming languages such as Python or Rust.Even if this new compiler option would only check for
IterableIterator
this would already solve this big pitfall in the type safety of TypeScript (thekeys
,values
andentries
methods ofArray.prototype
,Map.prototype
andSet.prototype
all returnIterableIterator
that doesn't work within
).Likewise just
const contained = 1 in numbers;
would also result in a type error if the compiler option was enabled. The new feature would be entirely backwards compatible since it would be opt-in.The text was updated successfully, but these errors were encountered: