microsoft / microsoft/TypeScript

Date.prototype.toJSON has incorrect return type

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

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

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

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.

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

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

はじめの一歩

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

調査の方向性

lib/lib.es5.d.ts の Date.prototype.toJSON の宣言を確認し、無効な Date と new Date() を使って提供された例を実行してください。TypeScript の型が結果が null になる可能性を警告しつつ、有効性が保証されている場合は string の結果を維持すれば、issue は完了です。

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

評価

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

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

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