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

[no-base-to-string] still many false positive with union values #1974

Closed
HolgerJeromin opened this issue May 4, 2020 · 2 comments · Fixed by #1979
Closed

[no-base-to-string] still many false positive with union values #1974

HolgerJeromin opened this issue May 4, 2020 · 2 comments · Fixed by #1979
Labels
bug Something isn't working has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@HolgerJeromin
Copy link

Thanks for #1969 :-)
Found a real bug while testing no-base-to-string :-)

Repro

{
  "rules": {
    "@typescript-eslint/no-base-to-string": ["error"]
  }
}
((myvar1: string | number | boolean) => {
    myvar1.toString();
})('');
((myvar1: string | number | boolean | null) => {
    myvar1.toString();
})('');
((myvar1: number | boolean | null) => {
    myvar1.toString();
})(true);

Expected Result
should lint without problems
Actual Result

warning @typescript-eslint/no-base-to-string : 'myvar1 may evaluate to '[object Object]' when stringified.

for all of them

Versions

package version
@typescript-eslint/eslint-plugin 2.31.0
@typescript-eslint/parser 2.31.0
TypeScript 3.8.3
ESLint 6.8.0
node X.Y.Z
npm X.Y.Z
@HolgerJeromin HolgerJeromin added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels May 4, 2020
@bradzacher
Copy link
Member

any union with null will fail, because null has no toString definition.
It was also specifically not handled on purpose, because null stringifies as "null", which is not a good thing to leak into strings - you should be handling the null state.

If you are okay with null leaking into your strings, you can ignore it with the ignoredTypeNames option.


Unions are handled in this rule implicitly - the rule gets all definitions of the toString method for the type. This "just works" for union types, and returns one declaration for each type.

Unfortunately, the previously workaround that's been implemented for the boolean type doesn't implicitly work for unions...

@bradzacher bradzacher added bug Something isn't working and removed triage Waiting for maintainers to take a look labels May 4, 2020
@auhsoja
Copy link
Contributor

auhsoja commented May 5, 2020

Not sure if I handled this in the most technically-correct way. However, I've submitted a PR that should make the errors go away if merged.

@bradzacher bradzacher added the has pr there is a PR raised to close this label May 5, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants