Repository: TypeScript
Issue: Deep readonly type with a nested tuple made deep writeable incorrectly extends `never`
Labels: Needs Investigation

# 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](https://www.typescriptlang.org/play?ts=4.9.4#code/C4TwDgpgBAIhFgOoCcCWwIEMBGAbCAPACoB8UAvFAN5QC0yWAJgPYB2uIUA2gApSqsoAawghmAMyhEAugC5Y8JGgw58xXtLIBfANwAoUJCgBJAM4A5CADcIyYmUpcZUCAA8MrRqe6trt6VAA-FDAyACu0PLimLimEPoG4NBwCACirpgAxsAAgsjImCAEecgu7hCe3pisIFzSADRQAMoAFpiQDtR6UNwA0vyCImKSJXIKaRnZre2EJVy9Dc1tkPNQAGTCohJLM5r6uolGKWDpWcDTkAQA8tgAVo0XEA7dUAAULz03t2UeXjuQHyCb0BPUePwqf2qIBBQPePXh8K+4MqUChdRhPWCx1O2RKhWudweyyeGKg8jhCMpZksNjs6UyuDCjEIQ22X0arMkjxIZDcv28oQipMxwMpYqgSL5EO8zDuEGywvhwSoioRqwEm2GEruY36UpRnP+0Cxihx52JBNu80Wj2tZHkvlp+nFlK0qrJ2tuioAlIqHX5kKTfWLg5T-bTAaGeuHbC9vQlMmxTMAoLLvpQVT1gMwwAAZPy4eSZ+GMTDATBFmEAWxARDCYHw8i4NGA9YAjA6wrhcI0+QV5AByAdQLTSQFunpurSo7yJ1jJ-SGaBIyjHFDoLB4QhL7ZpkiLpJGojMABqMVQpYwFC6WZz+ZshZvJbLmEClbFNbrDYgTZb7c73YjgEAA+UCsF2uDOvCByTnoBxLlAZ64BeZYQIwYKrqakzmjMlpEjMx5IShGD7noAD0ZHwgAeoEhzQBgybXpK5QokRl5oWCwSCpEUDRLE8TkZRPQ0XRIQQMmABMTF3MifwtneBZFlAl4VtQUCfvWjY+AG0g6COem6COQLcR6fFxPoFHUbRehzoxwAtAIADm8gMSmlBmfEUCWQAwm0rCOU5IQtNArCYFW0DbAhrmNOIzClG4YXfo02ZiaYjQahA6DBaUYSmJgjkhRAjmoQKwUhIe4jIMwVa8TEcQhMwIThBAyXBYI2BZEIDW1fxQA)

### 💻 Code

<!-- Please post the relevant code sample here as well-->
```ts
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.