From 6e6c3f935d82bd1d0f7b51a041f151a148dcc9f7 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Tue, 17 Nov 2026 12:46:31 -0800 Subject: [PATCH 1/2] Add a test for property access through a deep indexed access chain A chain of ten indexed accesses reaching the same type parameter is finite, but its base constraint is lost depending on which declaration is checked first, and accessing a property of it, or of the type parameter itself, reports a false error. For 65508 --- ...eepIndexedAccessChainConstraint.errors.txt | 29 +++++++++ .../deepIndexedAccessChainConstraint.symbols | 59 +++++++++++++++++++ .../deepIndexedAccessChainConstraint.types | 53 +++++++++++++++++ .../deepIndexedAccessChainConstraint.ts | 22 +++++++ 4 files changed, 163 insertions(+) create mode 100644 tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.errors.txt create mode 100644 tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.symbols create mode 100644 tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.types create mode 100644 tsc/testdata/tests/cases/compiler/deepIndexedAccessChainConstraint.ts diff --git a/tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.errors.txt b/tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.errors.txt new file mode 100644 index 0000000000000..1a07ff4cc9e05 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.errors.txt @@ -0,0 +1,29 @@ +deepIndexedAccessChainConstraint.ts(14,14): error TS2339: Property 'b' does not exist on type 'T'. +deepIndexedAccessChainConstraint.ts(14,20): error TS2339: Property 'b' does not exist on type 'T["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]'. + + +==== deepIndexedAccessChainConstraint.ts (2 errors) ==== + // Resolving the base constraint of a 10-level indexed access chain steps + // through nested object types that share the type parameter's recursion + // identity. Checking f resolves typeof victim while T's base constraint is + // uncached; in both victim and lone, base constraints resolve to Box (65508). + + interface Box { a: Box; b: string } + + const f: typeof victim = (x: U, y: { b: string }) => x.b; + + function victim( + x: T, + y: T["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"] + ): string { + return x.b + y.b; + ~ +!!! error TS2339: Property 'b' does not exist on type 'T'. + ~ +!!! error TS2339: Property 'b' does not exist on type 'T["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]'. + } + + function lone(y: U["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]): string { + return y.b; + } + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.symbols b/tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.symbols new file mode 100644 index 0000000000000..e7c1d2954bdd5 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.symbols @@ -0,0 +1,59 @@ +//// [tests/cases/compiler/deepIndexedAccessChainConstraint.ts] //// + +=== deepIndexedAccessChainConstraint.ts === +// Resolving the base constraint of a 10-level indexed access chain steps +// through nested object types that share the type parameter's recursion +// identity. Checking f resolves typeof victim while T's base constraint is +// uncached; in both victim and lone, base constraints resolve to Box (65508). + +interface Box { a: Box; b: string } +>Box : Symbol(Box, Decl(deepIndexedAccessChainConstraint.ts, 0, 0)) +>a : Symbol(Box.a, Decl(deepIndexedAccessChainConstraint.ts, 5, 15)) +>Box : Symbol(Box, Decl(deepIndexedAccessChainConstraint.ts, 0, 0)) +>b : Symbol(Box.b, Decl(deepIndexedAccessChainConstraint.ts, 5, 23)) + +const f: typeof victim = (x: U, y: { b: string }) => x.b; +>f : Symbol(f, Decl(deepIndexedAccessChainConstraint.ts, 7, 5)) +>victim : Symbol(victim, Decl(deepIndexedAccessChainConstraint.ts, 7, 72)) +>U : Symbol(U, Decl(deepIndexedAccessChainConstraint.ts, 7, 26)) +>Box : Symbol(Box, Decl(deepIndexedAccessChainConstraint.ts, 0, 0)) +>x : Symbol(x, Decl(deepIndexedAccessChainConstraint.ts, 7, 41)) +>U : Symbol(U, Decl(deepIndexedAccessChainConstraint.ts, 7, 26)) +>y : Symbol(y, Decl(deepIndexedAccessChainConstraint.ts, 7, 46)) +>b : Symbol(b, Decl(deepIndexedAccessChainConstraint.ts, 7, 51)) +>x.b : Symbol(Box.b, Decl(deepIndexedAccessChainConstraint.ts, 5, 23)) +>x : Symbol(x, Decl(deepIndexedAccessChainConstraint.ts, 7, 41)) +>b : Symbol(Box.b, Decl(deepIndexedAccessChainConstraint.ts, 5, 23)) + +function victim( +>victim : Symbol(victim, Decl(deepIndexedAccessChainConstraint.ts, 7, 72)) +>T : Symbol(T, Decl(deepIndexedAccessChainConstraint.ts, 9, 16)) +>Box : Symbol(Box, Decl(deepIndexedAccessChainConstraint.ts, 0, 0)) + + x: T, +>x : Symbol(x, Decl(deepIndexedAccessChainConstraint.ts, 9, 31)) +>T : Symbol(T, Decl(deepIndexedAccessChainConstraint.ts, 9, 16)) + + y: T["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"] +>y : Symbol(y, Decl(deepIndexedAccessChainConstraint.ts, 10, 9)) +>T : Symbol(T, Decl(deepIndexedAccessChainConstraint.ts, 9, 16)) + +): string { + return x.b + y.b; +>x : Symbol(x, Decl(deepIndexedAccessChainConstraint.ts, 9, 31)) +>y : Symbol(y, Decl(deepIndexedAccessChainConstraint.ts, 10, 9)) +} + +function lone(y: U["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]): string { +>lone : Symbol(lone, Decl(deepIndexedAccessChainConstraint.ts, 14, 1)) +>U : Symbol(U, Decl(deepIndexedAccessChainConstraint.ts, 16, 14)) +>Box : Symbol(Box, Decl(deepIndexedAccessChainConstraint.ts, 0, 0)) +>y : Symbol(y, Decl(deepIndexedAccessChainConstraint.ts, 16, 29)) +>U : Symbol(U, Decl(deepIndexedAccessChainConstraint.ts, 16, 14)) + + return y.b; +>y.b : Symbol(Box.b, Decl(deepIndexedAccessChainConstraint.ts, 5, 23)) +>y : Symbol(y, Decl(deepIndexedAccessChainConstraint.ts, 16, 29)) +>b : Symbol(Box.b, Decl(deepIndexedAccessChainConstraint.ts, 5, 23)) +} + diff --git a/tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.types b/tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.types new file mode 100644 index 0000000000000..79351d3ed48f2 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.types @@ -0,0 +1,53 @@ +//// [tests/cases/compiler/deepIndexedAccessChainConstraint.ts] //// + +=== deepIndexedAccessChainConstraint.ts === +// Resolving the base constraint of a 10-level indexed access chain steps +// through nested object types that share the type parameter's recursion +// identity. Checking f resolves typeof victim while T's base constraint is +// uncached; in both victim and lone, base constraints resolve to Box (65508). + +interface Box { a: Box; b: string } +>a : Box +>b : string + +const f: typeof victim = (x: U, y: { b: string }) => x.b; +>f : (x: T, y: T["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]) => string +>victim : (x: T, y: T["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]) => string +>(x: U, y: { b: string }) => x.b : (x: U, y: { b: string; }) => string +>x : U +>y : { b: string; } +>b : string +>x.b : string +>x : U +>b : string + +function victim( +>victim : (x: T, y: T["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]) => string + + x: T, +>x : T + + y: T["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"] +>y : T["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"] + +): string { + return x.b + y.b; +>x.b + y.b : any +>x.b : any +>x : T +>b : any +>y.b : any +>y : T["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"] +>b : any +} + +function lone(y: U["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]): string { +>lone : (y: U["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]) => string +>y : U["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"] + + return y.b; +>y.b : string +>y : U["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"] +>b : string +} + diff --git a/tsc/testdata/tests/cases/compiler/deepIndexedAccessChainConstraint.ts b/tsc/testdata/tests/cases/compiler/deepIndexedAccessChainConstraint.ts new file mode 100644 index 0000000000000..8a3c8d158ac2f --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/deepIndexedAccessChainConstraint.ts @@ -0,0 +1,22 @@ +// @strict: true +// @noEmit: true + +// Resolving the base constraint of a 10-level indexed access chain steps +// through nested object types that share the type parameter's recursion +// identity. Checking f resolves typeof victim while T's base constraint is +// uncached; in both victim and lone, base constraints resolve to Box (65508). + +interface Box { a: Box; b: string } + +const f: typeof victim = (x: U, y: { b: string }) => x.b; + +function victim( + x: T, + y: T["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"] +): string { + return x.b + y.b; +} + +function lone(y: U["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]): string { + return y.b; +} From 6d470c72f6c928a6c8b98e55dc3bc6ead6ffe1b1 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Tue, 17 Nov 2026 12:48:45 -0800 Subject: [PATCH 2/2] Count only expanding types as repeats in base constraint walks Computing a base constraint gives up past ten levels of nesting once a type with the same recursion identity repeats, as a repeating pattern could be instantiated without end. The object types of a chain of indexed accesses, such as T["a"]["a"]["a"], all share the identity of T, so a chain of ten or more gave up, and accessing a property of it failed. Base constraints already resolved are not walked again, so whether a walk reached the limit depended on which declaration was checked first. A type repeats only if it is no older than the type with the same identity on the stack, as each step of an expanding instantiation is built from the step before it, whereas the object type of an indexed access is a part of it. As in isDeeplyNestedType, type ids give the order in which types were created. Fixes 65508 --- tsc/internal/checker/checker.go | 32 ++++++++++++++++--- ...eepIndexedAccessChainConstraint.errors.txt | 29 ----------------- .../deepIndexedAccessChainConstraint.symbols | 4 +++ .../deepIndexedAccessChainConstraint.types | 10 +++--- 4 files changed, 36 insertions(+), 39 deletions(-) delete mode 100644 tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.errors.txt diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index 41e76dc37b730..36382bbb3bdc3 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -28308,7 +28308,29 @@ func (c *Checker) getBaseConstraintOfType(t *Type) *Type { return nil } -func (c *Checker) getResolvedBaseConstraint(t *Type, stack []RecursionId) *Type { +// baseConstraintFrame records a type's recursion identity and type ID on the +// getResolvedBaseConstraint stack. +type baseConstraintFrame struct { + identity RecursionId + id TypeId +} + +// repeatsInBaseConstraintWalk reports whether the candidate type shares a +// recursion identity with a frame on stack and has an equal or higher type ID. +// Type IDs are assigned in creation order: expanding instantiations +// (Foo -> Foo>) create types with higher IDs, while walking into +// component types with the same identity, such as the object type of an +// indexed access (T["a"]["a"] -> T["a"]), moves to lower IDs and terminates. +func repeatsInBaseConstraintWalk(identity RecursionId, id TypeId, stack []baseConstraintFrame) bool { + for _, f := range stack { + if f.identity == identity && id >= f.id { + return true + } + } + return false +} + +func (c *Checker) getResolvedBaseConstraint(t *Type, stack []baseConstraintFrame) *Type { constrained := t.AsConstrainedType() if constrained == nil { return t @@ -28327,8 +28349,8 @@ func (c *Checker) getResolvedBaseConstraint(t *Type, stack []RecursionId) *Type // yet triggered the deeply nested limiter. We have no test cases that actually get to 50 levels of // nesting, so it is effectively just a safety stop. identity := getRecursionIdentity(t) - if len(stack) < 10 || len(stack) < 50 && !slices.Contains(stack, identity) { - constraint = c.computeBaseConstraint(c.getSimplifiedType(t /*writing*/, false), append(stack, identity)) + if len(stack) < 10 || len(stack) < 50 && !repeatsInBaseConstraintWalk(identity, t.id, stack) { + constraint = c.computeBaseConstraint(c.getSimplifiedType(t /*writing*/, false), append(stack, baseConstraintFrame{identity, t.id})) } if !c.popTypeResolution() { if t.flags&TypeFlagsTypeParameter != 0 { @@ -28351,7 +28373,7 @@ func (c *Checker) getResolvedBaseConstraint(t *Type, stack []RecursionId) *Type return constraint } -func (c *Checker) computeBaseConstraint(t *Type, stack []RecursionId) *Type { +func (c *Checker) computeBaseConstraint(t *Type, stack []baseConstraintFrame) *Type { switch { case t.flags&TypeFlagsTypeParameter != 0: constraint := c.getConstraintFromTypeParameter(t) @@ -28454,7 +28476,7 @@ func (c *Checker) computeBaseConstraint(t *Type, stack []RecursionId) *Type { return t } -func (c *Checker) getNextBaseConstraint(t *Type, stack []RecursionId) *Type { +func (c *Checker) getNextBaseConstraint(t *Type, stack []baseConstraintFrame) *Type { if t == nil { return nil } diff --git a/tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.errors.txt b/tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.errors.txt deleted file mode 100644 index 1a07ff4cc9e05..0000000000000 --- a/tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.errors.txt +++ /dev/null @@ -1,29 +0,0 @@ -deepIndexedAccessChainConstraint.ts(14,14): error TS2339: Property 'b' does not exist on type 'T'. -deepIndexedAccessChainConstraint.ts(14,20): error TS2339: Property 'b' does not exist on type 'T["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]'. - - -==== deepIndexedAccessChainConstraint.ts (2 errors) ==== - // Resolving the base constraint of a 10-level indexed access chain steps - // through nested object types that share the type parameter's recursion - // identity. Checking f resolves typeof victim while T's base constraint is - // uncached; in both victim and lone, base constraints resolve to Box (65508). - - interface Box { a: Box; b: string } - - const f: typeof victim = (x: U, y: { b: string }) => x.b; - - function victim( - x: T, - y: T["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"] - ): string { - return x.b + y.b; - ~ -!!! error TS2339: Property 'b' does not exist on type 'T'. - ~ -!!! error TS2339: Property 'b' does not exist on type 'T["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]'. - } - - function lone(y: U["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]): string { - return y.b; - } - \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.symbols b/tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.symbols index e7c1d2954bdd5..faaa14d10c054 100644 --- a/tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.symbols +++ b/tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.symbols @@ -40,8 +40,12 @@ function victim( ): string { return x.b + y.b; +>x.b : Symbol(Box.b, Decl(deepIndexedAccessChainConstraint.ts, 5, 23)) >x : Symbol(x, Decl(deepIndexedAccessChainConstraint.ts, 9, 31)) +>b : Symbol(Box.b, Decl(deepIndexedAccessChainConstraint.ts, 5, 23)) +>y.b : Symbol(Box.b, Decl(deepIndexedAccessChainConstraint.ts, 5, 23)) >y : Symbol(y, Decl(deepIndexedAccessChainConstraint.ts, 10, 9)) +>b : Symbol(Box.b, Decl(deepIndexedAccessChainConstraint.ts, 5, 23)) } function lone(y: U["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]): string { diff --git a/tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.types b/tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.types index 79351d3ed48f2..1bc741135670b 100644 --- a/tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.types +++ b/tsc/testdata/baselines/reference/compiler/deepIndexedAccessChainConstraint.types @@ -32,13 +32,13 @@ function victim( ): string { return x.b + y.b; ->x.b + y.b : any ->x.b : any +>x.b + y.b : string +>x.b : string >x : T ->b : any ->y.b : any +>b : string +>y.b : string >y : T["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"] ->b : any +>b : string } function lone(y: U["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]["a"]): string {