Skip to content

String(number) does not return ${number} typeΒ #43673

@qwelias

Description

@qwelias

Bug Report

πŸ”Ž Search Terms

string constructor number type template literal

πŸ•— Version & Regression Information

4.2.3

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

const stringNumber: `${number}` = String(123)

πŸ™ Actual behavior

Error: Type 'string' is not assignable to type '${number}'.(2322)

πŸ™‚ Expected behavior

No error.

Why

String, according to ES (String(value), and ToString(argument)), uses toString method on passed in values, so ideally the fix should include only changes on Number interface, but currently it's String's def does not care about toString, so these two signature would have to be generic:

// lib.es5.d.ts
interface StringConstructor {
    new(value?: any): String;
    (value?: any): string;
}

And this one would need to be generic as well as the type of returned string would depend on the radix.

// lib.es5.d.ts
interface Number {
    toString(radix?: number): string;
}

This approach would make String easily extensible and compatible with template literal types, but I'm not sure how drastic of a change it is.

Most likely would be related to #42996

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptIn DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions