From 68e3eaeea8a427031971bd51d45418502f01b92e Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Thu, 12 Nov 2026 17:16:53 -0800 Subject: [PATCH 1/2] Add test for declared members read while instantiating base types For 64605 --- .../circularDeclaredPropertyAccess.errors.txt | 52 +++++++++ .../circularDeclaredPropertyAccess.symbols | 109 ++++++++++++++++++ .../circularDeclaredPropertyAccess.types | 55 +++++++++ .../circularDeclaredPropertyAccess.ts | 22 ++++ 4 files changed, 238 insertions(+) create mode 100644 tsc/testdata/baselines/reference/compiler/circularDeclaredPropertyAccess.errors.txt diff --git a/tsc/testdata/baselines/reference/compiler/circularDeclaredPropertyAccess.errors.txt b/tsc/testdata/baselines/reference/compiler/circularDeclaredPropertyAccess.errors.txt new file mode 100644 index 0000000000000..6c6198c5e8261 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/circularDeclaredPropertyAccess.errors.txt @@ -0,0 +1,52 @@ +circularDeclaredPropertyAccess.ts(41,31): error TS5115: Instantiations of the following types appear infinitely circular: 'Internals', 'RecordInput'. + + +==== circularDeclaredPropertyAccess.ts (1 errors) ==== + // https://github.com/microsoft/TypeScript/issues/64605 + + interface Internals { input: I } + interface Schema { _zod: Internals } + type RecordInput = V extends unknown ? Record : never; + interface RecordSchema extends Schema { _zod: Internals> } + interface UnionInternals extends Internals {} + interface UnionSchema extends Schema { _zod: UnionInternals } + type JsonValue = { [k: string]: JsonValue }; + type _Json = UnionSchema<[RecordSchema]>; + type _JsonInternals = _Json["_zod"]; + interface JsonInternals extends _JsonInternals { input: JsonValue } + interface Json extends _Json { _zod: JsonInternals } + + // Ensure getDeclaredPropertyOfResolvingType works for inherited declared members + + interface A { kind: string; m: this["kind"] } + interface B { m: "a" } + type AB = A & B; + + interface D extends AB { kind: "a" } + + interface E extends D {} + + // Declared members are available while the base types are instantiated, which + // can need one of them through a type argument referring back to the type. + + namespace BaseTypeArguments { + interface Internals { input: I; optin?: "optional" | "defaulted" | undefined } + interface Schema { _zod: Internals } + type OptionalIn = { _zod: { optin: "optional" | "defaulted" } }; + type IsOptionalIn = T extends OptionalIn ? true : false; + type RecordInput = [V] extends [OptionalIn] ? Partial> : Record; + interface RecordInternals extends Internals> { optin?: "optional" | undefined } + interface RecordSchema extends Schema { _zod: RecordInternals } + interface UnionInternals extends Internals { + optin: IsOptionalIn extends false ? "optional" | "defaulted" | undefined : "optional" | "defaulted"; + } + interface UnionSchema extends Schema { _zod: UnionInternals } + type JsonValue = { [k: string]: JsonValue }; + type _Json = UnionSchema<[RecordSchema]>; + ~~~~~~~~~~~~~~~~~~ +!!! error TS5115: Instantiations of the following types appear infinitely circular: 'Internals', 'RecordInput'. + type _JsonInternals = _Json["_zod"]; + interface JsonInternals extends _JsonInternals { input: JsonValue } + interface Json extends _Json { _zod: JsonInternals } + } + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/circularDeclaredPropertyAccess.symbols b/tsc/testdata/baselines/reference/compiler/circularDeclaredPropertyAccess.symbols index a05b24d6aca1c..ad01868965b7e 100644 --- a/tsc/testdata/baselines/reference/compiler/circularDeclaredPropertyAccess.symbols +++ b/tsc/testdata/baselines/reference/compiler/circularDeclaredPropertyAccess.symbols @@ -100,3 +100,112 @@ interface E extends D {} >E : Symbol(E, Decl(circularDeclaredPropertyAccess.ts, 20, 36)) >D : Symbol(D, Decl(circularDeclaredPropertyAccess.ts, 18, 16)) +// Declared members are available while the base types are instantiated, which +// can need one of them through a type argument referring back to the type. + +namespace BaseTypeArguments { +>BaseTypeArguments : Symbol(BaseTypeArguments, Decl(circularDeclaredPropertyAccess.ts, 22, 24)) + + interface Internals { input: I; optin?: "optional" | "defaulted" | undefined } +>Internals : Symbol(Internals, Decl(circularDeclaredPropertyAccess.ts, 27, 29)) +>I : Symbol(I, Decl(circularDeclaredPropertyAccess.ts, 28, 24)) +>input : Symbol(Internals.input, Decl(circularDeclaredPropertyAccess.ts, 28, 38)) +>I : Symbol(I, Decl(circularDeclaredPropertyAccess.ts, 28, 24)) +>optin : Symbol(Internals.optin, Decl(circularDeclaredPropertyAccess.ts, 28, 48)) + + interface Schema { _zod: Internals } +>Schema : Symbol(Schema, Decl(circularDeclaredPropertyAccess.ts, 28, 95)) +>_zod : Symbol(Schema._zod, Decl(circularDeclaredPropertyAccess.ts, 29, 22)) +>Internals : Symbol(Internals, Decl(circularDeclaredPropertyAccess.ts, 27, 29)) + + type OptionalIn = { _zod: { optin: "optional" | "defaulted" } }; +>OptionalIn : Symbol(OptionalIn, Decl(circularDeclaredPropertyAccess.ts, 29, 40)) +>_zod : Symbol(_zod, Decl(circularDeclaredPropertyAccess.ts, 30, 23)) +>optin : Symbol(optin, Decl(circularDeclaredPropertyAccess.ts, 30, 31)) + + type IsOptionalIn = T extends OptionalIn ? true : false; +>IsOptionalIn : Symbol(IsOptionalIn, Decl(circularDeclaredPropertyAccess.ts, 30, 68)) +>T : Symbol(T, Decl(circularDeclaredPropertyAccess.ts, 31, 22)) +>Schema : Symbol(Schema, Decl(circularDeclaredPropertyAccess.ts, 28, 95)) +>T : Symbol(T, Decl(circularDeclaredPropertyAccess.ts, 31, 22)) +>OptionalIn : Symbol(OptionalIn, Decl(circularDeclaredPropertyAccess.ts, 29, 40)) + + type RecordInput = [V] extends [OptionalIn] ? Partial> : Record; +>RecordInput : Symbol(RecordInput, Decl(circularDeclaredPropertyAccess.ts, 31, 78)) +>V : Symbol(V, Decl(circularDeclaredPropertyAccess.ts, 32, 21)) +>Schema : Symbol(Schema, Decl(circularDeclaredPropertyAccess.ts, 28, 95)) +>V : Symbol(V, Decl(circularDeclaredPropertyAccess.ts, 32, 21)) +>OptionalIn : Symbol(OptionalIn, Decl(circularDeclaredPropertyAccess.ts, 29, 40)) +>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --)) +>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) +>V : Symbol(V, Decl(circularDeclaredPropertyAccess.ts, 32, 21)) +>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) +>V : Symbol(V, Decl(circularDeclaredPropertyAccess.ts, 32, 21)) + + interface RecordInternals extends Internals> { optin?: "optional" | undefined } +>RecordInternals : Symbol(RecordInternals, Decl(circularDeclaredPropertyAccess.ts, 32, 149)) +>V : Symbol(V, Decl(circularDeclaredPropertyAccess.ts, 33, 30)) +>Schema : Symbol(Schema, Decl(circularDeclaredPropertyAccess.ts, 28, 95)) +>Internals : Symbol(Internals, Decl(circularDeclaredPropertyAccess.ts, 27, 29)) +>RecordInput : Symbol(RecordInput, Decl(circularDeclaredPropertyAccess.ts, 31, 78)) +>V : Symbol(V, Decl(circularDeclaredPropertyAccess.ts, 33, 30)) +>optin : Symbol(RecordInternals.optin, Decl(circularDeclaredPropertyAccess.ts, 33, 83)) + + interface RecordSchema extends Schema { _zod: RecordInternals } +>RecordSchema : Symbol(RecordSchema, Decl(circularDeclaredPropertyAccess.ts, 33, 116)) +>V : Symbol(V, Decl(circularDeclaredPropertyAccess.ts, 34, 27)) +>Schema : Symbol(Schema, Decl(circularDeclaredPropertyAccess.ts, 28, 95)) +>Schema : Symbol(Schema, Decl(circularDeclaredPropertyAccess.ts, 28, 95)) +>_zod : Symbol(RecordSchema._zod, Decl(circularDeclaredPropertyAccess.ts, 34, 61)) +>RecordInternals : Symbol(RecordInternals, Decl(circularDeclaredPropertyAccess.ts, 32, 149)) +>V : Symbol(V, Decl(circularDeclaredPropertyAccess.ts, 34, 27)) + + interface UnionInternals extends Internals { +>UnionInternals : Symbol(UnionInternals, Decl(circularDeclaredPropertyAccess.ts, 34, 88)) +>T : Symbol(T, Decl(circularDeclaredPropertyAccess.ts, 35, 29)) +>Schema : Symbol(Schema, Decl(circularDeclaredPropertyAccess.ts, 28, 95)) +>Internals : Symbol(Internals, Decl(circularDeclaredPropertyAccess.ts, 27, 29)) +>T : Symbol(T, Decl(circularDeclaredPropertyAccess.ts, 35, 29)) + + optin: IsOptionalIn extends false ? "optional" | "defaulted" | undefined : "optional" | "defaulted"; +>optin : Symbol(UnionInternals.optin, Decl(circularDeclaredPropertyAccess.ts, 35, 105)) +>IsOptionalIn : Symbol(IsOptionalIn, Decl(circularDeclaredPropertyAccess.ts, 30, 68)) +>T : Symbol(T, Decl(circularDeclaredPropertyAccess.ts, 35, 29)) + } + interface UnionSchema extends Schema { _zod: UnionInternals } +>UnionSchema : Symbol(UnionSchema, Decl(circularDeclaredPropertyAccess.ts, 37, 5)) +>T : Symbol(T, Decl(circularDeclaredPropertyAccess.ts, 38, 26)) +>Schema : Symbol(Schema, Decl(circularDeclaredPropertyAccess.ts, 28, 95)) +>Schema : Symbol(Schema, Decl(circularDeclaredPropertyAccess.ts, 28, 95)) +>_zod : Symbol(UnionSchema._zod, Decl(circularDeclaredPropertyAccess.ts, 38, 71)) +>UnionInternals : Symbol(UnionInternals, Decl(circularDeclaredPropertyAccess.ts, 34, 88)) +>T : Symbol(T, Decl(circularDeclaredPropertyAccess.ts, 38, 26)) + + type JsonValue = { [k: string]: JsonValue }; +>JsonValue : Symbol(JsonValue, Decl(circularDeclaredPropertyAccess.ts, 38, 97)) +>k : Symbol(k, Decl(circularDeclaredPropertyAccess.ts, 39, 24)) +>JsonValue : Symbol(JsonValue, Decl(circularDeclaredPropertyAccess.ts, 38, 97)) + + type _Json = UnionSchema<[RecordSchema]>; +>_Json : Symbol(_Json, Decl(circularDeclaredPropertyAccess.ts, 39, 48)) +>UnionSchema : Symbol(UnionSchema, Decl(circularDeclaredPropertyAccess.ts, 37, 5)) +>RecordSchema : Symbol(RecordSchema, Decl(circularDeclaredPropertyAccess.ts, 33, 116)) +>Json : Symbol(Json, Decl(circularDeclaredPropertyAccess.ts, 42, 71)) + + type _JsonInternals = _Json["_zod"]; +>_JsonInternals : Symbol(_JsonInternals, Decl(circularDeclaredPropertyAccess.ts, 40, 51)) +>_Json : Symbol(_Json, Decl(circularDeclaredPropertyAccess.ts, 39, 48)) + + interface JsonInternals extends _JsonInternals { input: JsonValue } +>JsonInternals : Symbol(JsonInternals, Decl(circularDeclaredPropertyAccess.ts, 41, 40)) +>_JsonInternals : Symbol(_JsonInternals, Decl(circularDeclaredPropertyAccess.ts, 40, 51)) +>input : Symbol(JsonInternals.input, Decl(circularDeclaredPropertyAccess.ts, 42, 52)) +>JsonValue : Symbol(JsonValue, Decl(circularDeclaredPropertyAccess.ts, 38, 97)) + + interface Json extends _Json { _zod: JsonInternals } +>Json : Symbol(Json, Decl(circularDeclaredPropertyAccess.ts, 42, 71)) +>_Json : Symbol(_Json, Decl(circularDeclaredPropertyAccess.ts, 39, 48)) +>_zod : Symbol(Json._zod, Decl(circularDeclaredPropertyAccess.ts, 43, 34)) +>JsonInternals : Symbol(JsonInternals, Decl(circularDeclaredPropertyAccess.ts, 41, 40)) +} + diff --git a/tsc/testdata/baselines/reference/compiler/circularDeclaredPropertyAccess.types b/tsc/testdata/baselines/reference/compiler/circularDeclaredPropertyAccess.types index 911ec7f3388a0..19f275e02f6a4 100644 --- a/tsc/testdata/baselines/reference/compiler/circularDeclaredPropertyAccess.types +++ b/tsc/testdata/baselines/reference/compiler/circularDeclaredPropertyAccess.types @@ -52,3 +52,58 @@ interface D extends AB { kind: "a" } interface E extends D {} +// Declared members are available while the base types are instantiated, which +// can need one of them through a type argument referring back to the type. + +namespace BaseTypeArguments { + interface Internals { input: I; optin?: "optional" | "defaulted" | undefined } +>input : I +>optin : "defaulted" | "optional" | undefined + + interface Schema { _zod: Internals } +>_zod : Internals + + type OptionalIn = { _zod: { optin: "optional" | "defaulted" } }; +>OptionalIn : OptionalIn +>_zod : { optin: "optional" | "defaulted"; } +>optin : "defaulted" | "optional" + + type IsOptionalIn = T extends OptionalIn ? true : false; +>IsOptionalIn : IsOptionalIn +>true : true +>false : false + + type RecordInput = [V] extends [OptionalIn] ? Partial> : Record; +>RecordInput : RecordInput + + interface RecordInternals extends Internals> { optin?: "optional" | undefined } +>optin : "optional" | undefined + + interface RecordSchema extends Schema { _zod: RecordInternals } +>_zod : RecordInternals + + interface UnionInternals extends Internals { + optin: IsOptionalIn extends false ? "optional" | "defaulted" | undefined : "optional" | "defaulted"; +>optin : IsOptionalIn extends false ? "defaulted" | "optional" | undefined : "defaulted" | "optional" +>false : false + } + interface UnionSchema extends Schema { _zod: UnionInternals } +>_zod : UnionInternals + + type JsonValue = { [k: string]: JsonValue }; +>JsonValue : JsonValue +>k : string + + type _Json = UnionSchema<[RecordSchema]>; +>_Json : _Json + + type _JsonInternals = _Json["_zod"]; +>_JsonInternals : UnionInternals<[RecordSchema]> + + interface JsonInternals extends _JsonInternals { input: JsonValue } +>input : JsonValue + + interface Json extends _Json { _zod: JsonInternals } +>_zod : JsonInternals +} + diff --git a/tsc/testdata/tests/cases/compiler/circularDeclaredPropertyAccess.ts b/tsc/testdata/tests/cases/compiler/circularDeclaredPropertyAccess.ts index cd60247771008..2db479c7be709 100644 --- a/tsc/testdata/tests/cases/compiler/circularDeclaredPropertyAccess.ts +++ b/tsc/testdata/tests/cases/compiler/circularDeclaredPropertyAccess.ts @@ -23,3 +23,25 @@ type AB = A & B; interface D extends AB { kind: "a" } interface E extends D {} + +// Declared members are available while the base types are instantiated, which +// can need one of them through a type argument referring back to the type. + +namespace BaseTypeArguments { + interface Internals { input: I; optin?: "optional" | "defaulted" | undefined } + interface Schema { _zod: Internals } + type OptionalIn = { _zod: { optin: "optional" | "defaulted" } }; + type IsOptionalIn = T extends OptionalIn ? true : false; + type RecordInput = [V] extends [OptionalIn] ? Partial> : Record; + interface RecordInternals extends Internals> { optin?: "optional" | undefined } + interface RecordSchema extends Schema { _zod: RecordInternals } + interface UnionInternals extends Internals { + optin: IsOptionalIn extends false ? "optional" | "defaulted" | undefined : "optional" | "defaulted"; + } + interface UnionSchema extends Schema { _zod: UnionInternals } + type JsonValue = { [k: string]: JsonValue }; + type _Json = UnionSchema<[RecordSchema]>; + type _JsonInternals = _Json["_zod"]; + interface JsonInternals extends _JsonInternals { input: JsonValue } + interface Json extends _Json { _zod: JsonInternals } +} From dd12f7bdabdd4ddd6aa7ca37f6e3e5c1394a0978 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Thu, 12 Nov 2026 17:18:47 -0800 Subject: [PATCH 2/2] Read declared members while instantiating base types A class or interface makes its declared members available while its members are resolved, so that resolving them can read one without resolving them again. They were only available once its base types were instantiated, yet instantiating them can need a declared member, when a type argument of a base type refers back to the type. Zod 4.5 through 4.6.5 declares interface $ZodRecordInternals extends $ZodTypeInternals<..., $InferZodRecordInput> { optin?: "optional" | undefined; } where $InferZodRecordInput checks whether Value is optional by reading optin, which for z.json() leads back to the optin that $ZodRecordInternals declares. Resolving its members again instantiated the same base types until the depth limit, reporting TS5115 for every use of z.json() in an array or a record, and for Zod's own declarations without skipLibCheck. The declared members are now available as soon as the base types are known, before they are instantiated. Fixes 64605 --- tsc/internal/checker/checker.go | 5 +- .../circularDeclaredPropertyAccess.errors.txt | 52 ------------------- 2 files changed, 4 insertions(+), 53 deletions(-) delete mode 100644 tsc/testdata/baselines/reference/compiler/circularDeclaredPropertyAccess.errors.txt diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index f1e5564580f3c..4aa08e9e10a75 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -19693,6 +19693,9 @@ func (c *Checker) resolveObjectTypeMembers(t *Type) { indexInfos = c.instantiateIndexInfos(resolved.declaredIndexInfos, mapper) } baseTypes := c.getBaseTypes(source) + // Declared members are available while the base types are instantiated, + // as their type arguments can refer back to this type. + c.resolvingObjectTypeStack = append(c.resolvingObjectTypeStack, ResolvingObjectType{t: t, mapper: mapper}) instantiatedBaseTypes := baseTypes thisArgument := core.LastOrNil(typeArguments) if len(baseTypes) != 0 && thisArgument != nil { @@ -19701,7 +19704,7 @@ func (c *Checker) resolveObjectTypeMembers(t *Type) { instantiatedBaseTypes[i] = c.getTypeWithThisArgument(c.instantiateType(baseType, mapper), thisArgument, false /*needsApparentType*/) } } - c.resolvingObjectTypeStack = append(c.resolvingObjectTypeStack, ResolvingObjectType{t: t, mapper: mapper, baseTypes: instantiatedBaseTypes}) + c.resolvingObjectTypeStack[len(c.resolvingObjectTypeStack)-1].baseTypes = instantiatedBaseTypes // Member layout must be computed before inherited signatures, which may circularly depend on it layout := c.getMemberLayout(t, instantiatedBaseTypes) for _, instantiatedBaseType := range instantiatedBaseTypes { diff --git a/tsc/testdata/baselines/reference/compiler/circularDeclaredPropertyAccess.errors.txt b/tsc/testdata/baselines/reference/compiler/circularDeclaredPropertyAccess.errors.txt deleted file mode 100644 index 6c6198c5e8261..0000000000000 --- a/tsc/testdata/baselines/reference/compiler/circularDeclaredPropertyAccess.errors.txt +++ /dev/null @@ -1,52 +0,0 @@ -circularDeclaredPropertyAccess.ts(41,31): error TS5115: Instantiations of the following types appear infinitely circular: 'Internals', 'RecordInput'. - - -==== circularDeclaredPropertyAccess.ts (1 errors) ==== - // https://github.com/microsoft/TypeScript/issues/64605 - - interface Internals { input: I } - interface Schema { _zod: Internals } - type RecordInput = V extends unknown ? Record : never; - interface RecordSchema extends Schema { _zod: Internals> } - interface UnionInternals extends Internals {} - interface UnionSchema extends Schema { _zod: UnionInternals } - type JsonValue = { [k: string]: JsonValue }; - type _Json = UnionSchema<[RecordSchema]>; - type _JsonInternals = _Json["_zod"]; - interface JsonInternals extends _JsonInternals { input: JsonValue } - interface Json extends _Json { _zod: JsonInternals } - - // Ensure getDeclaredPropertyOfResolvingType works for inherited declared members - - interface A { kind: string; m: this["kind"] } - interface B { m: "a" } - type AB = A & B; - - interface D extends AB { kind: "a" } - - interface E extends D {} - - // Declared members are available while the base types are instantiated, which - // can need one of them through a type argument referring back to the type. - - namespace BaseTypeArguments { - interface Internals { input: I; optin?: "optional" | "defaulted" | undefined } - interface Schema { _zod: Internals } - type OptionalIn = { _zod: { optin: "optional" | "defaulted" } }; - type IsOptionalIn = T extends OptionalIn ? true : false; - type RecordInput = [V] extends [OptionalIn] ? Partial> : Record; - interface RecordInternals extends Internals> { optin?: "optional" | undefined } - interface RecordSchema extends Schema { _zod: RecordInternals } - interface UnionInternals extends Internals { - optin: IsOptionalIn extends false ? "optional" | "defaulted" | undefined : "optional" | "defaulted"; - } - interface UnionSchema extends Schema { _zod: UnionInternals } - type JsonValue = { [k: string]: JsonValue }; - type _Json = UnionSchema<[RecordSchema]>; - ~~~~~~~~~~~~~~~~~~ -!!! error TS5115: Instantiations of the following types appear infinitely circular: 'Internals', 'RecordInput'. - type _JsonInternals = _Json["_zod"]; - interface JsonInternals extends _JsonInternals { input: JsonValue } - interface Json extends _Json { _zod: JsonInternals } - } - \ No newline at end of file