-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Rule proposal: "no-misused-in" warn against using in
on values that aren't indexable
#5677
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
What about "no-misused-in"? |
If there are more types that we think it's invalid to use Perhaps it's worth just making this error on One caveat to the above is that maybe it's worth allowing it for object unions in case someone wants to use it to narrow like |
|
@Josh-Cena could you clarify with some example code how it would be unsafe to use with a union? |
TS allows using |
Yeah I guess structural typing is a bitch there and it allows some dodgy code! |
Correct—but in an actual program it's rarely that trivial, and the |
in
on Set
and Map
in
on values that aren't indexable
When moving from js objects to this.__object = {};
// ...
this.__object['objectPropertyname']; this.__map = new Map();
// ...
this.__map['objectPropertyname']; // ops Disclaimer: we use ts config option |
@HolgerJeromin - originally I was thinking that the best plan for this rule was ensuring correct usage of ES6 collections - but I think there's more value in specifically just handling the Given that TS has a compiler option which would handle the index access usecase in collections - I don't think it makes sense for us to build a rule in this plugin for it. |
A note from #5474: this rule should cover the case of arrays, too. |
I just wanna get a background on what is exactly is a misuse of I wanna work on this, It seems complex for me as a new one in ESLint & ast... (But I'll try) I know one of them which is type |
@mahdi-farnia does the issue's description contain enough information to answer your question? |
@bradzacher Know i do. So Yes!
|
I was going to submit a new rule proposal just now for
Something that would ban usage of the if ('x' in (undefined as any)) { But maybe comment #1251768783 by @bradzacher alludes to
Or should this rule proposal for |
I think that would be the best thing yeah - one rule for safe |
Before You File a Proposal Please Confirm You Have Done The Following...
My proposal is suitable for this project
Background
In languages like python use the
in
operator to check for key existence in sets and maps. Polyglot engineers may make the mistake to usein
in JS.With this change to TS to be released in 4.9, the
in
operator will now refine any object type by intersecting it withRecord<key, unknown>
. This means you could get some funky behaviour that previously was not allowed.For example:
Currently in TS 4.8 this errors with
Element implicitly has an 'any' type because type 'Map<string, unknown>' has no index signature. Did you mean to call 'map.get'? (7052)
After the above PR, this no longer errors at all because the type of
map
is refined toMap<string, unknown> & Record<'woopsie', unknown>
, sotypeof map['woopsie'] === unknown
.Rule Description
In the vast, vast majority of cases you wouldn't want to allow
in
to be used. I can think of the following cases that you would want to allowin
:{}
object
Record<K, V>
)I think behind a (default false) flag we could also allow
in
to be used against types if and only if the type (or one of the types if it is a union) has that property.Fail Cases
With the "allow known keys in objects" flag turned OFF:
Pass Cases
With the "allow known keys in objects" flag turned ON:
The text was updated successfully, but these errors were encountered: