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

https://typescript-eslint.io/play/#ts=5.3.3&fileType=.ts&code=BQOwpg7gBAsghgB2ASmQOgC4HsDKGBOAliAOYoDcQA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1oCMBDZRLXxdk%2BKkwDm6KImjQO0SODABfECqA&tsconfig=&tokens=false

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)

Most upvoted comments

Aside: this might be the first issue promoted from evaluating community engagement to accepting prs we’ve ever had. Certainly the first I can remember. The process works! 🚀

Results of the poll:

image slack image slack

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 @@toStringTag and 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

  1. See a function in my work codebase getting flagged by linter, literally this:

    function makeParameterString() {
        const parameterMap = new Map();
        parameterMap.set('x', 'y');
        parameterMap.set('y', 'z');
        // set a whole bunch of parameters
        return parameterMap.toString();
    }
    
  2. Think, wow what a brazen violation of the rule, they must know something I don’t - is the rule correct here?

  3. 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]

  4. Think - hmm, well, the rule wasn’t perfectly correct there, so, maybe there’s situations where it legitimately does do something useful.

  5. Spend 30 mins reading up on Object.prototype.toString and toStringTag.

  6. Start looking through the node docs to figure out whether it magically stringifies differently in Node. It doesn’t

  7. Think, hmm, I doubted the rule for longer than I ought to have, just because it had this technically incorrect detail.

  8. Think, hey, I could probably fix that to save time for other people like me who are pathologically anal about details

  9. 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 tweak

  10. here 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 toString is a bad one.

This behaviour in particular is driven by the existence of the Symbol.toStringTag property on a class. If it exists then the string of that property replaces Object.

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].