question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add missing `toString` declarations for base types that have them

See original GitHub issue

Search Terms

toString, RegExp, Regular Expression, Boolean, Error

Suggestion

In @typescript-eslint, we have a rule no-base-to-string. This rule ensures that any value that’s being coerced to a string has a toString method defined on it. This is to help catch cases where you accidentally coerce something to a string, and end up with an [object Object] in your strings.

This rule has a pretty simple implementation - it just gets the declarations of the toString method on the type, and ensures that none of the declarations belong to the Object type. The logic relies upon the fact that every type that actually has a toString method explicitly declares a toString method.

A few of our users ran into cases that were reporting errors for them, even though they shouldn’t (https://github.com/typescript-eslint/typescript-eslint/issues/1655). There’s one that specifically has caused people some issues: Boolean/boolean.

I eyeballed the docs and the types, and I am pretty certain there are only 3 types that are missing a toString declaration:

Related

PR: #37839

Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript/JavaScript code
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:12
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
cnshenjcommented, May 27, 2020

In this case, the built-in declaration is wrong, why it shouldn’t be fixed? Boolean type has a toString method: Boolean.prototype.toString(). Even without considering linting, I can’t write true.toString(), which is perfectly valid JavaScript and (should be) TypeScript code.

3reactions
Oaphicommented, Apr 9, 2021

Regarding the missing definition for the Boolean interface, it would also be nice if the return type of the method was defined as a string literal union "true" | "false" and not just string (as in the PR) since even the spec considers “true” and “false” to be the only possible outcomes of step 2 of the method algorithm.

This would allow patterns like this (valid in JS) without having to resort to declaration merging in user code:

type SorterMap<T> = Record<"true" | "false", (a: T, b: T) => number>;

const makeTdStrSorter = ({ up = true }) => <
  T extends string
>(
  a: T,
  b: T
) => {
  const dirMap: SorterMap<T> = {
    true: (a, b) => (a < b ? -1 : a > b ? 1 : 0),
    false: (a, b) => (a > b ? -1 : a < b ? 1 : 0),
  };

  return dirMap[up.toString()](a, b); //expression of type 'string' can't be used to index type 'SorterMap<T>'
};

I do understand and appreciate the concern that changes to the standard library may cause things to break, but this is about adding something that should’ve probably been there from the get-go, so if it does break something, it is better to know it now than further down the road.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - How to print <?xml version="1.0"?> using XDocument
By using XDeclaration. This will add the declaration. But with ToString() you will not get the desired output. You need to use XDocument....
Read more >
How to override the ToString method - C# Programming Guide
Declare a ToString method with the following modifiers and return type: C# Copy. public override string ToString(){} · Implement the method so ...
Read more >
A tour of the Dart language
This page shows you how to use each major Dart feature, from variables and operators to classes and libraries, with the assumption that...
Read more >
How To Create Custom Types in TypeScript - DigitalOcean
Now that you have gone through some examples of creating your own custom type with a fixed number of properties, next you'll try...
Read more >
Java Generated Code | Protocol Buffers - Google Developers
If you are using proto3, it also adds the special value UNRECOGNIZED to the enum type. The values of the generated enum type...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found