microsoft / microsoft/TypeScript
`[...T[], T]` does not extend `[T, ...T[]]`
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
🔎 Search Terms
homogeneous tuple, non-empty array, assert not empty
🕗 Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about array/tuple
⏯ Playground Link
💻 Code
type Test = [...1[], 1] extends [1, ...1[]] ? true : false;
// ^? type Test = false
In context:
const isEmpty = <T>(xs: T[]): xs is [] => !xs.length
const last = <T>(xs: [T, ...T[]]) => xs[xs.length - 1];
type Foo = [] | [number, ...number[]];
declare const xs: Foo;
declare const ys: number[]
if(!isEmpty(xs)) {
const zs: Foo = [...ys, last(xs)] as const;
// ~~
// Type '[...number[], number]' is not assignable to type 'Foo'.
// Type '[...number[], number]' is not assignable to type '[number, ...number[]]'.
// Source provides no match for required element at position 0 in target.(2322)
}
🙁 Actual behavior
For a homogeneous tuple, [...T[], T] and [T, ...T[]] are the same thing.
TS fails to recognise that in the in-context example: it is saying that [...ys, last(xs)] could be missing a required number but it can't because last(xs) is the required number that it is looking for. The only issue is that it is of type [...number[], number] and since [...T[], U] is not the same thing as [T, ...U[]], It chokes.
🙂 Expected behavior
Test should be true and zs: Foo should not be a type error.
Additional information about the issue
I wish !isEmpty could narrow T[] to [T, ...T[]].
Using a isNotEmpty helper with Foo defined as number[] would solve this specific case but in the wild it would force me to call it like so !isNotEmpty(xs) in many places, which is terrible for readability.
It would be possible in the in-context example to define Foo as [] | [number, ...number[]] | [...number[], number] and last as <T>(xs: [T, ...T[]] | [...T[], T]) =>T but nobody in the world is doing that and I would be splitting hair alone in my basement not being able to use any library.
I think I trip over this more often in recursive logic.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンクされた TypeScript Playground と簡略化した条件型 Test から始め、次に Foo、isEmpty、last を使ったコンテキスト内の例と比較します。Test が true と評価され、zs: Foo への代入で型エラーが報告されなくなれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 35/100