Skip to content
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

fix(valid-typeof): allow comparison with variables other than undefined, null, Object #570

Closed
wants to merge 4 commits into from

Conversation

magurotuna
Copy link
Member

Fixes #566

Please see #566 (comment) before reviewing because this fix relaxes the current restriction of valid-typeof rule, being against the previous discussion at #38.

In addition, I've done with the following things:

@bartlomieju
Copy link
Member

@kitsonk @lucacasonato @nayeemrmn what do you think about this PR?

@kitsonk
Copy link
Contributor

kitsonk commented Dec 31, 2020

It would be good to get an example of what this would trigger on (e.g. a "bad" example). I am confused on what it would add over and above existing TypeScript strict type checking.

@magurotuna
Copy link
Member Author

@kitsonk
Basically, this fix allows our valid-typeof to behave in the same way as ESLint's valid-typeof with requireStringLiterals: false. To put it simply, it reports "typo"s in string literals that are compared with typeof xxx values, while anything else is always okay.
The following code would be "bad" examples (taken from https://eslint.org/docs/rules/valid-typeof). These are "typo"s.

typeof foo === "strnig"
typeof foo == "undefimed"
typeof bar != "nunber"
typeof bar !== "fucntion"

There's one difference between ESLint's valid-typeof with requireStringLiterals: false and the implementation in this PR.
ESLint allows comparison with things like undefined literal, while the implementation in this PR doesn't allow them. For example, the following cases are treated as valid by ESLint, but invalid by ours.

typeof foo === undefined
typeof foo == null
typeof foo !== Object

I think TypeScript checking also treat these kinds of comparison as valid, but they are most likely mistakes by a programmer, so it'd be good to lint it.

In summary, this PR changes two things:

@kitsonk
Copy link
Contributor

kitsonk commented Dec 31, 2020

Of those, only these two don't produce errors in TypeScript (which is somewhat disappointing, as I would have thought they would have had overlap type errors):

typeof a === undefined
typeof a == null

I am of the opinion we should avoid overlap of such things, personally.

@magurotuna
Copy link
Member Author

@kitsonk
Oh okay, then valid-typeof rule seems almost useless in the context of TypeScript.
So it means that our valid-typeof rule should check comparison with undefined and null only.

One concern I have is whether or not we need to care about JavaScript. If we avoided overlap with TypeScript, erroneous codes like typeof foo === "strnig", if written in JavaScript, wouldn't be checked at all. Therefore I think that the minimum overlap to lint JavaScript well is necessary.

Base automatically changed from master to main February 14, 2021 13:44
@ry
Copy link
Member

ry commented Aug 7, 2021

@magurotuna What's the status of this PR? Are you still trying to get this merged? If so, please rebase, otherwise please close.

@magurotuna
Copy link
Member Author

Closing because it's stale. More consideration and discussion will be needed.

@magurotuna magurotuna closed this Aug 10, 2021
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

Successfully merging this pull request may close these issues.

Comparing a literal string union variable to typeof throws a valid-typeof error
5 participants