microsoft / microsoft/TypeScript
String(number) does not return `${number}` type
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia riproducendo l'assegnazione nel TypeScript Playground collegato, poi esamina le definizioni di StringConstructor e Number in lib.es5.d.ts e leggi la issue correlata #42996. Il lavoro è completato quando si raggiunge un comportamento type-safe concordato per String(123) e Number.toString con i tipi di literal template, supportato da test appropriati del compilatore.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript, typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100