typescript-eslint: Bug: [no-base-to-string] Maps result in [object Map] not [object Object]
Before You File a Bug Report Please Confirm You Have Done The Following…
- I have tried restarting my IDE and the issue persists.
- I have updated to the latest version of the packages.
- I have searched for related issues and found none that matched my issue.
- I have read the FAQ and my problem is not listed.
Playground Link
Repro Code
(new Map()).toString();
ESLint Config
module.exports = {
"rules": {
"@typescript-eslint/no-base-to-string": "error"
}
}
tsconfig
{
"compilerOptions": {
// ...
}
}
Expected Result
I expect it to flag with error message 'new Map()' will evaluate to '[object Map]' when stringified. 1:2 - 1:11
Actual Result
It flags with error message 'new Map()' will evaluate to '[object Object]' when stringified. 1:2 - 1:11
Additional Info
new Set()).toString() has the same nitpick.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 15 (15 by maintainers)
Aside: this might be the first issue promoted from
evaluating community engagementtoaccepting prswe’ve ever had. Certainly the first I can remember. The process works! 🚀Results of the poll:
People guessed right but for the wrong reason.
I’m +1 on an error message more nuanced than the current one, but I don’t have great suggestions, maybe “It will use Object’s default stringification format ([object Object]), which is likely not useful for display”? Vague but correct.
I’m -0.5 to test
@@toStringTagand change the error message based on that. We are only marginally towards making the error message “more correct” while greatly complicating our code for an edge case.lol I am very appreciative to be ranked amongst the “super duper power users” ❤️
In any case, though, I would say that my position here is really just based on the fact that I get unreasonably worked up when people in general, and software frameworks/docs specifically, make imprecise or technically incorrect claims, even when the overall point is basically correct. So, my thought process in this case was
See a function in my work codebase getting flagged by linter, literally this:
Think, wow what a brazen violation of the rule, they must know something I don’t - is the rule correct here?
Stringify Map in the browser - does not come out as “[object Object]” (though still useless). Believe me, I did not know in advance that it would be
[object Map]instead of[object Object]Think - hmm, well, the rule wasn’t perfectly correct there, so, maybe there’s situations where it legitimately does do something useful.
Spend 30 mins reading up on Object.prototype.toString and toStringTag.
Start looking through the node docs to figure out whether it magically stringifies differently in Node. It doesn’t
Think, hmm, I doubted the rule for longer than I ought to have, just because it had this technically incorrect detail.
Think, hey, I could probably fix that to save time for other people like me who are pathologically anal about details
Realize, oh, yeah, changing the code to actually report
[object Map]is a way bigger pain than it’s worth, but agree with discussion that it could be amelilorated by wording tweakhere we are, my case is made, call is up to maintainers to decide on. No worries if you close it out!
The text is slightly wrong but the intent is correct - the
toStringis a bad one.This behaviour in particular is driven by the existence of the
Symbol.toStringTagproperty on a class. If it exists then the string of that property replacesObject.However this property isn’t declared on any of the types so it’s impossible for us to know whether or not the “base toString” would return
[object Object]or[object Foo].