Skip to content

Missing Boolean.toString definition and loose valueOf definition #60517

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

Open
Akindin opened this issue Nov 16, 2024 · 3 comments
Open

Missing Boolean.toString definition and loose valueOf definition #60517

Akindin opened this issue Nov 16, 2024 · 3 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@Akindin
Copy link

Akindin commented Nov 16, 2024

⚙ Compilation target

ES2017

⚙ Library

lib.es5.d.ts

Missing / Incorrect Definition

interface Boolean {
    toString<T extends boolean>(this: T): `${T}`
    toString(this: Boolean): `${boolean}`
    valueOf<T extends boolean>(this: T): T
    valueOf(this: Boolean): boolean
}

TS Playground

Related issues #30225, #38347

Sample Code

const bool = true as boolean;


let a: `${boolean}` = true.toString();
let b: `${boolean}` = false.toString();
let c: `${boolean}` = bool.toString();
let d: `${boolean}` = new Boolean().toString();
let e: `${boolean}` = Boolean().toString();

let f: true = true.valueOf();
let g: false = false.valueOf();

Documentation Link

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean/toString
https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-boolean.prototype.tostring
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean/valueOf
https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-boolean.prototype.valueof

@jcalz
Copy link
Contributor

jcalz commented Nov 16, 2024

new Boolean().toString() and true.valueOf() make me want to cry. Is there any real world example that couldn't be served equally well by just "false" or `${false}` as const or, for valueOf, just, like... true? Why would the suggested modification be a good idea for all TS code everywhere and not just merged as-needed for those who want it?

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Nov 17, 2024
@kirkwaiblinger
Copy link

kirkwaiblinger commented Dec 9, 2024

Not sure whether this constitutes any motivation to change the current definitions, but noting as a matter of interest that we do have to work around this at typescript-eslint:

https://github.com/typescript-eslint/typescript-eslint/blob/d03270fe0d8d05852d0602659e52e80531f92847/packages/eslint-plugin/src/rules/no-base-to-string.ts#L188-L194

@kirkwaiblinger
Copy link

Oh, sorry, I see that this exactly was already discussed in the referenced issue #38347

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants