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.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 } +}