microsoft / microsoft/TypeScript

Array inheritance in ES6 declaration file (lib.es6.d.ts)

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

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

Domain: lib.d.ts Help Wanted Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

ES6 supports Array inheritance, but this is not reflected in the TypeScript declaration file (lib.es6.d.ts).

Array functions like 'filter' and 'slice' return an array of the subclass, so the following should compile in TypeScript:

class MyArray extends Array {
    get size() { return this.length; }
}

var x = new MyArray(10).slice();
x.size;

The functions 'reverse', 'concat', 'slice', 'splice', 'filter' should return with the type 'this'.
The 'map' function returns an instance of the subtype too, but it I don't know how that could be represented in TypeScript, as it would require a generic parameter in the return type, something like:
map<U>(callback: (value: T, index: number, array: this) => U): this<U>

Another issue is the static constructors of the Array class, this should compile too:

var x = MyArray.of(1,2,3);
var y = MyArray.from([1,2,3]);
x.size;
y.size;

There are also some additional complications added by the @@species Symbol, which can be used to change the type signature of the subclass to not use 'this' as the return value for functions like 'filter' and 'slice'. So for the following example, the subclass would work how the return types are defined in the declaration file currently:

class MyArray2 extends Array {
    static get [Symbol.species]() { return Array; }
}

Chrome and Node.js both already implement the array inheritance features shown here.

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

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

はじめの一歩

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

調査の方向性

lib.es6.d.ts から始め、issue の例と照らし合わせて Array のメソッドおよび静的コンストラクターの宣言を調べます。戻り値の型でサブクラス化、map、Symbol.species をどのように表現すべきかを判断し、提供されている MyArray と MyArray2 のケースが意図どおりにコンパイルされることを確認します。

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

評価

技術スタック
javascript, typescript
領域
compilers
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

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

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