microsoft / microsoft/TypeScript
Improve `Array.from(tuple)` and `[...tuple]`
オープン
まだ誰も着手していません。
Domain: lib.d.ts
In Discussion
Suggestion
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
Suggestion
Array.from(tuple) and [...tuple] should preserve individual types that made up tuple.
Proposal
Array.from (Simple)
Add this overload to Array.from:
interface ArrayConstructor {
from<T extends any[]> (array: T): T
}
Caveats:
- The above definition preserves everything including unrelated properties that do not belong to
Array.prototypewhilst actualArray.fromdiscards them. (for instance, if input hasfoo: 'bar', output array will also hasfoo: 'bar').
Array.from (Complete)
Fix above caveats.
interface ArrayConstructor {
from<T extends any[]> (array: T): CloneArray<T>
}
type CloneArray<T extends any[]> = {
[i in number & keyof T]: T[i]
} & {
length: T['length']
} & any[]
Spread operator
declare const tuple: [0, 1, 2] & { foo: 'bar' }
// $ExpectType [string, string, 0, 1, 2, string, string]
const clone = ['a', 'b', ...tuple, 'c', 'd']
Note that typeof clone does not contain { foo: 'bar' }.
Use Cases
- Clone a tuple without losing type information.
Examples
Clone a tuple
const a: [0, 1, 2] = [0, 1, 2]
// $ExpectType [0, 1, 2]
const b = Array.from(a)
Clone a generic tuple
function clone<T extends any[]> (a: T): T {
return Array.from(a)
}
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. new expression-level syntax)
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
この issue ではリポジトリのファイル、テスト、エントリポイントが指定されていないため、まず Array.from と spread の提案、およびそれらにリンクされた例を読んでください。完了条件は、説明されている Array.from と spread のケースについて、JavaScript のランタイム動作を変更せずにタプルの要素型と長さの型が保持されることです。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 35/100