microsoft / microsoft/TypeScript

Deep readonly type with a nested tuple made deep writeable incorrectly extends `never`

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

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

Needs Investigation
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

Bug Report

🔎 Search Terms

never tuple
readonly tuple
change name of type
change result of type

🕗 Version & Regression Information
  • This changed between versions 4.4.4 (correct) and 4.5.5 (incorrect) and 4.6.4 (correct) and >= 4.7.4, <= 5.2.2 (incorrect) and 5.3.2 (correct)
⏯ Playground Link

Playground link with relevant code

💻 Code
type DeepWriteable<T> = { -readonly [P in keyof T]: DeepWriteable<T[P]> };
type IsNever<T> = [T] extends [never] ? true : false;

type DeepExactArray<Arr extends any[], Shape> = {
  [K in keyof Arr]: DeepExactShape<Arr[K], Shape[K & keyof Shape]>;
};
type DeepExactShape<Obj, Shape> =
  (
    Obj extends Shape
    ? (
      Shape extends any
      ? (
        Obj extends any[]
        ? DeepExactArray<Obj, Shape>
        : (
          IsNever<Exclude<keyof Obj, keyof Shape>> extends true
          ? (
            Obj extends object
            ? {
              [K in keyof Obj]: K extends keyof Shape ? DeepExactShape<Obj[K], Shape[K]> : never;
            }
            : Obj
          )
          : never
        )
      )
      : never
    )
    : never
  );

const obj = {
  topLevel: {
    data: {
      myTuple: [{ tup1: null, extra: '' }]
    }
  }
} as const;
type Obj = DeepWriteable<typeof obj>;
type ShapeToValidate = {
  topLevel: {
    data?: {
      myTuple: [{ tup1: null }] | null;
    };
  }
};
type ValidatedShape = DeepExactShape<Obj, ShapeToValidate>;
//   ^? { topLevel: { data: { myTuple: [never]; }; }; }
type test = Obj extends ValidatedShape ? true : false;
//    ^? type test = true;
type test2 = Obj extends { topLevel: { data: { myTuple: [never]; }; }; } ? true : false;
//    ^? type test2 = false;

const thing: test = false; // Changing the name of type test, for example, to tes, in either usage negates the type from false to true, then back to false
🙁 Actual behavior

A type with a multi-level nested tuple incorrectly extends itself but with a slight modification: the element of the tuple is replaced with never, but only when the type is created the using above DeepExactShape type. When using the same type with the never replacement, but explicitly written out instead, it correctly does not extend.

In addition, when removing the as const statement, it correctly does not extend. When changing the nested depth of the tuple from 3 to 2 (removing the topLevel property), it correctly does not extend.

Finally, when changing the name of type test, for example, to tes, in either usage negates the type from false to true, then back to false as visible in the two-slash query and when hovering over the type (in 4.7.4 and above)

🙂 Expected behavior

In both cases, the initial type, Obj, should not extend either the created type (from DeepExactShape), or the explicitly defined type, with, or without, the as const statement included.

Changing the name of a type should not result in the value of the type being different.

The degree of nesting (2 vs 3) should not change whether the type extends the created type.

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

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

はじめの一歩

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

調査の方向性

リンクされた TypeScript Playground から始め、レポートに記載されているバージョン全体でネストされたタプルの例を再現します。型名、ネストの深さ、as const を変更しながら、ValidatedShape、test、test2 の計算された型を比較します。これらのバリエーションが一貫して期待される拡張なしの結果を生成すれば完了です。

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

評価

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

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

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