microsoft / microsoft/TypeScript

Type instantiation is excessively deep with tuple spread

オープン
#54,910 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

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

Domain: Conditional Types Help Wanted Possible Improvement
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

Bug Report

🔎 Search Terms
  • 2889 spread
🕗 Version & Regression Information
  • This is the behavior in every version I tried (4.1.5 to 5.2.0 nightly), and I reviewed the FAQ for entries about: recursion
⏯ Playground Link

Playground link with relevant code

💻 Code
type NTuple<T, N extends number, Acc extends T[] = []> =
  N extends Acc['length']
  ? Acc
  : NTuple<T, N, [...Acc, T]>

type Pair = NTuple<number, 2>;
//   ^? type Pair = [number, number]

type SpreadN<A extends number> = [...NTuple<any, A>]
//                               ~~~~~~~~~~~~~~~~~~~
// Type instantiation is excessively deep and possibly infinite. (2589)
🙁 Actual behavior

I'm getting the "Type instantiation is excessively deep and possibly infinite. (2589)" error on what should be the identity.

🙂 Expected behavior

I'd expect this to just work. SpreadN should produce the same results as NTuple. It's surprising to me that this error is produced on the definition of SpreadN, rather than when it's instantiated with an overly large value of N.

I think this may have something to do with distribution over unions because when I reverse the conditional, the code works fine:

type NTuple2<T, N extends number, Acc extends T[] = []> =
  Acc['length'] extends N  // note that the check is reversed
  ? Acc
  : NTuple2<T, N, [...Acc, T]>

type SpreadN2<A extends number> = [...NTuple<any, A>]  // ok
type Pair2 = SpreadN2<2>;
//   ^? type Pair2 = [any, any]

It's not clear to me why reversing that conditional would matter, but it does have the effect of preventing distribution over unions, which is not desirable:

type T1 = NTuple<string, 2 | 3>;
//   ^? type T1 = [string, string] | [string, string, string]
type T2 = NTuple2<string, 2 | 3>;
//   ^? type T2 = [string, string]

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

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

はじめの一歩

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

調査の方向性

まず、リンク先の TypeScript Playground にあるタプルスプレッドの例を再現し、NTuple と NTuple2 を比較します。これには、それらのユニオンに対する挙動も含まれます。SpreadN の定義でエラー 2589 が報告されなくなり、NTuple が期待される分配されたユニオン結果を引き続き保持していれば完了です。

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

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

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

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