microsoft / microsoft/TypeScript

String(number) does not return `${number}` type

Open
#43,673 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: lib.d.ts In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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 https://github.com/microsoft/TypeScript/issues/42996

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the assignment in the linked TypeScript Playground, then inspect the StringConstructor and Number definitions in lib.es5.d.ts and read related issue #42996. Done means reaching an agreed type-safe behavior for String(123) and Number.toString with template literal types, supported by appropriate compiler tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.