microsoft / microsoft/TypeScript
Disallow values of type 'symbol' from being passed to 'Number' function/constructor or 'String' constructor
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
Suggestion
Change the NumberConstructor and StringConstructor interfaces to disallow passing values of type Symbol. Currently it accepts values of type any.
Attempting to call Number(Symbol()), new Number(Symbol()), or new String(Symbol()) all throw TypeErrors at runtime.
Note: String(Symbol()) is fine - only new String(Symbol()) will throw.
Use Cases
Any time a value of type any is passed to Number, there is a risk of that value being a symbol and throwing a TypeError.
Object keys are explicitly allowed to be of type symbol (as number | string | symbol). Passing an arbitrary object key to Number can throw a TypeError as well.
Examples
Checking if a value can be coerced to a number
I just ran into this issue when checking if an object key could be a valid array index. The following should be an error as it will throw if value is of type symbol.
function keyCanBeArrayIndex(value: string | number | symbol): boolean {
return !Number.isNaN(Number(value));
}
Having this be invalid would have forced me to write something like:
function canBeArrayIndex(value: string | number | symbol): boolean {
try {
return !Number.isNaN(Number(value));
} catch {
return false;
}
}
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、TypeScript のライブラリ定義内にある NumberConstructor および StringConstructor インターフェース宣言を見つけ、コンストラクター引数のチェックに関する既存のテストを確認します。Number(Symbol())、new Number(Symbol())、String(Symbol())、new String(Symbol()) の意図された違いを確認し、意図したコンパイル時診断を示すテストカバレッジを追加します。完了条件は、issue で指定された箇所でのみ symbol 値が拒否され、出力される JavaScript が変更されないことです。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- javascript, typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 45/100