microsoft / microsoft/TypeScript
Date.prototype.toJSON has incorrect return type
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
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.
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
Leggi la dichiarazione di Date.prototype.toJSON in lib/lib.es5.d.ts, quindi esegui l'esempio fornito con un Date non valido e con new Date(). L'issue è completata quando i tipi TypeScript avvertono che il risultato potrebbe essere null, preservando un risultato string quando la validità è garantita.
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