microsoft / microsoft/TypeScript

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

オープン
#43,673 コメント 2 件 リアクション 2 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Domain: lib.d.ts In Discussion Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、リンク先の TypeScript Playground で課題を再現し、次に lib.es5.d.ts 内の StringConstructor と Number の定義を調べ、関連する issue #42996 を読みます。String(123) と Number.toString の template literal types に対して合意された型安全な動作に到達し、適切な compiler tests によって裏付けられれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript, typescript
領域
compilers
issue の種類
バグ
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。