microsoft / microsoft/TypeScript
Date.prototype.toJSON has incorrect return type
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
Bug Report
Date.prototype.toJSON() can return a string or null according to the spec https://tc39.es/ecma262/#sec-date.prototype.tolocaledatestring. For example new Date("").toJSON() returns null.
Currently, the return type of toJSON() is specified as a string:
https://github.com/microsoft/TypeScript/blob/131875bb849c0a9c56c26d09881453aace0cbfed/lib/lib.es5.d.ts#L879
🕗 Version & Regression Information
Typescript 4.2.3
💻 Code
new Date("").toJSON().slice(0,5);
produces an error at runtime since new Date("").toJSON() is null, but doesn't produce a typescript error because new Date("").toJSON() is assumed to be string.
🙂 Expected behavior
TypeScript should warn about the potentially null value when an arbitrary string is passed into Date. However, if the Date object can be guaranteed to be valid (for example new Date()), the return type of toJSON() should be string.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Lies die Deklaration für Date.prototype.toJSON in lib/lib.es5.d.ts und führe das bereitgestellte Beispiel mit einem ungültigen Date und mit new Date() aus. Das Issue ist abgeschlossen, wenn die TypeScript-Typen vor dem potenziell null seienden Ergebnis warnen und dabei ein string-Ergebnis erhalten bleibt, wenn die Gültigkeit garantiert ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- javascript, typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100