microsoft / microsoft/TypeScript
Improve support for Symbol.toPrimitive
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
Presently, TS limits index types to 'string', 'number', 'symbol', or 'any'. That's fine, but consider the following:
class Foo {
[Symbol.toPrimitive](): number {
return 1;
}
}
var x = ['a','b','c'];
var f = new Foo();
x[f];
We give the error an index expression argument must be of type ... on the array access.
Foo is for all intents and purposes a wrapper around the number 1 (close to new Number(1)), yet it cannot be used to index an array, since Foo itself is not of type number, string, symbol, or any. We could do extra type-checking to see if an object has a user-defined Symbol.toPrimitive and consider its return types when checking type-coercing operations (after all, if you've opted in to using Symbol.toPrimitve it's probably part of your goal to gain this behavior), such as indexing or mathematics operators.
Building on this, we currently don't support the coercion of Number or String classes to their respective primitive types (which are a specialization of the above issue but which applies to ES5 and ES3 output). Presently one can't do this:
function add(a: string, b: string): number {
var x = new Number(a);
var y = new Number(b);
return x + y;
}
add('10e4', '0x8');
Because since neither x nor y is the 'number' (or 'string') primitive type, the addition operation is not allowed. Though it's still valid js (and without the usage of the Number class, the addition would result in '10e40x8'!).
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
issue にあるインデックス式と加算の例から始め、次にインデックス引数と算術オペランドの型チェック規則を追ってください。Symbol.toPrimitive と Number/String ラッパーの型強制を一貫して考慮しつつ、無効な操作を許可しない状態になれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- javascript, typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100