Skip to content

Commit bd5148a

Browse files
authored
Read declared members while instantiating base types (#65479)
1 parent 3f95358 commit bd5148a

4 files changed

Lines changed: 190 additions & 1 deletion

File tree

‎tsc/internal/checker/checker.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19701,6 +19701,9 @@ func (c *Checker) resolveObjectTypeMembers(t *Type) {
1970119701
indexInfos = c.instantiateIndexInfos(resolved.declaredIndexInfos, mapper)
1970219702
}
1970319703
baseTypes := c.getBaseTypes(source)
19704+
// Declared members are available while the base types are instantiated,
19705+
// as their type arguments can refer back to this type.
19706+
c.resolvingObjectTypeStack = append(c.resolvingObjectTypeStack, ResolvingObjectType{t: t, mapper: mapper})
1970419707
instantiatedBaseTypes := baseTypes
1970519708
thisArgument := core.LastOrNil(typeArguments)
1970619709
if len(baseTypes) != 0 && thisArgument != nil {
@@ -19709,7 +19712,7 @@ func (c *Checker) resolveObjectTypeMembers(t *Type) {
1970919712
instantiatedBaseTypes[i] = c.getTypeWithThisArgument(c.instantiateType(baseType, mapper), thisArgument, false /*needsApparentType*/)
1971019713
}
1971119714
}
19712-
c.resolvingObjectTypeStack = append(c.resolvingObjectTypeStack, ResolvingObjectType{t: t, mapper: mapper, baseTypes: instantiatedBaseTypes})
19715+
c.resolvingObjectTypeStack[len(c.resolvingObjectTypeStack)-1].baseTypes = instantiatedBaseTypes
1971319716
// Member layout must be computed before inherited signatures, which may circularly depend on it
1971419717
layout := c.getMemberLayout(t, instantiatedBaseTypes)
1971519718
for _, instantiatedBaseType := range instantiatedBaseTypes {

‎tsc/testdata/baselines/reference/compiler/circularDeclaredPropertyAccess.symbols‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,112 @@ interface E extends D {}
100100
>E : Symbol(E, Decl(circularDeclaredPropertyAccess.ts, 20, 36))
101101
>D : Symbol(D, Decl(circularDeclaredPropertyAccess.ts, 18, 16))
102102

103+
// Declared members are available while the base types are instantiated, which
104+
// can need one of them through a type argument referring back to the type.
105+
106+
namespace BaseTypeArguments {
107+
>BaseTypeArguments : Symbol(BaseTypeArguments, Decl(circularDeclaredPropertyAccess.ts, 22, 24))
108+
109+
interface Internals<I = unknown> { input: I; optin?: "optional" | "defaulted" | undefined }
110+
>Internals : Symbol(Internals, Decl(circularDeclaredPropertyAccess.ts, 27, 29))
111+
>I : Symbol(I, Decl(circularDeclaredPropertyAccess.ts, 28, 24))
112+
>input : Symbol(Internals.input, Decl(circularDeclaredPropertyAccess.ts, 28, 38))
113+
>I : Symbol(I, Decl(circularDeclaredPropertyAccess.ts, 28, 24))
114+
>optin : Symbol(Internals.optin, Decl(circularDeclaredPropertyAccess.ts, 28, 48))
115+
116+
interface Schema { _zod: Internals }
117+
>Schema : Symbol(Schema, Decl(circularDeclaredPropertyAccess.ts, 28, 95))
118+
>_zod : Symbol(Schema._zod, Decl(circularDeclaredPropertyAccess.ts, 29, 22))
119+
>Internals : Symbol(Internals, Decl(circularDeclaredPropertyAccess.ts, 27, 29))
120+
121+
type OptionalIn = { _zod: { optin: "optional" | "defaulted" } };
122+
>OptionalIn : Symbol(OptionalIn, Decl(circularDeclaredPropertyAccess.ts, 29, 40))
123+
>_zod : Symbol(_zod, Decl(circularDeclaredPropertyAccess.ts, 30, 23))
124+
>optin : Symbol(optin, Decl(circularDeclaredPropertyAccess.ts, 30, 31))
125+
126+
type IsOptionalIn<T extends Schema> = T extends OptionalIn ? true : false;
127+
>IsOptionalIn : Symbol(IsOptionalIn, Decl(circularDeclaredPropertyAccess.ts, 30, 68))
128+
>T : Symbol(T, Decl(circularDeclaredPropertyAccess.ts, 31, 22))
129+
>Schema : Symbol(Schema, Decl(circularDeclaredPropertyAccess.ts, 28, 95))
130+
>T : Symbol(T, Decl(circularDeclaredPropertyAccess.ts, 31, 22))
131+
>OptionalIn : Symbol(OptionalIn, Decl(circularDeclaredPropertyAccess.ts, 29, 40))
132+
133+
type RecordInput<V extends Schema> = [V] extends [OptionalIn] ? Partial<Record<string, V["_zod"]["input"]>> : Record<string, V["_zod"]["input"]>;
134+
>RecordInput : Symbol(RecordInput, Decl(circularDeclaredPropertyAccess.ts, 31, 78))
135+
>V : Symbol(V, Decl(circularDeclaredPropertyAccess.ts, 32, 21))
136+
>Schema : Symbol(Schema, Decl(circularDeclaredPropertyAccess.ts, 28, 95))
137+
>V : Symbol(V, Decl(circularDeclaredPropertyAccess.ts, 32, 21))
138+
>OptionalIn : Symbol(OptionalIn, Decl(circularDeclaredPropertyAccess.ts, 29, 40))
139+
>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --))
140+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
141+
>V : Symbol(V, Decl(circularDeclaredPropertyAccess.ts, 32, 21))
142+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
143+
>V : Symbol(V, Decl(circularDeclaredPropertyAccess.ts, 32, 21))
144+
145+
interface RecordInternals<V extends Schema> extends Internals<RecordInput<V>> { optin?: "optional" | undefined }
146+
>RecordInternals : Symbol(RecordInternals, Decl(circularDeclaredPropertyAccess.ts, 32, 149))
147+
>V : Symbol(V, Decl(circularDeclaredPropertyAccess.ts, 33, 30))
148+
>Schema : Symbol(Schema, Decl(circularDeclaredPropertyAccess.ts, 28, 95))
149+
>Internals : Symbol(Internals, Decl(circularDeclaredPropertyAccess.ts, 27, 29))
150+
>RecordInput : Symbol(RecordInput, Decl(circularDeclaredPropertyAccess.ts, 31, 78))
151+
>V : Symbol(V, Decl(circularDeclaredPropertyAccess.ts, 33, 30))
152+
>optin : Symbol(RecordInternals.optin, Decl(circularDeclaredPropertyAccess.ts, 33, 83))
153+
154+
interface RecordSchema<V extends Schema> extends Schema { _zod: RecordInternals<V> }
155+
>RecordSchema : Symbol(RecordSchema, Decl(circularDeclaredPropertyAccess.ts, 33, 116))
156+
>V : Symbol(V, Decl(circularDeclaredPropertyAccess.ts, 34, 27))
157+
>Schema : Symbol(Schema, Decl(circularDeclaredPropertyAccess.ts, 28, 95))
158+
>Schema : Symbol(Schema, Decl(circularDeclaredPropertyAccess.ts, 28, 95))
159+
>_zod : Symbol(RecordSchema._zod, Decl(circularDeclaredPropertyAccess.ts, 34, 61))
160+
>RecordInternals : Symbol(RecordInternals, Decl(circularDeclaredPropertyAccess.ts, 32, 149))
161+
>V : Symbol(V, Decl(circularDeclaredPropertyAccess.ts, 34, 27))
162+
163+
interface UnionInternals<T extends readonly Schema[]> extends Internals<T[number]["_zod"]["input"]> {
164+
>UnionInternals : Symbol(UnionInternals, Decl(circularDeclaredPropertyAccess.ts, 34, 88))
165+
>T : Symbol(T, Decl(circularDeclaredPropertyAccess.ts, 35, 29))
166+
>Schema : Symbol(Schema, Decl(circularDeclaredPropertyAccess.ts, 28, 95))
167+
>Internals : Symbol(Internals, Decl(circularDeclaredPropertyAccess.ts, 27, 29))
168+
>T : Symbol(T, Decl(circularDeclaredPropertyAccess.ts, 35, 29))
169+
170+
optin: IsOptionalIn<T[number]> extends false ? "optional" | "defaulted" | undefined : "optional" | "defaulted";
171+
>optin : Symbol(UnionInternals.optin, Decl(circularDeclaredPropertyAccess.ts, 35, 105))
172+
>IsOptionalIn : Symbol(IsOptionalIn, Decl(circularDeclaredPropertyAccess.ts, 30, 68))
173+
>T : Symbol(T, Decl(circularDeclaredPropertyAccess.ts, 35, 29))
174+
}
175+
interface UnionSchema<T extends readonly Schema[]> extends Schema { _zod: UnionInternals<T> }
176+
>UnionSchema : Symbol(UnionSchema, Decl(circularDeclaredPropertyAccess.ts, 37, 5))
177+
>T : Symbol(T, Decl(circularDeclaredPropertyAccess.ts, 38, 26))
178+
>Schema : Symbol(Schema, Decl(circularDeclaredPropertyAccess.ts, 28, 95))
179+
>Schema : Symbol(Schema, Decl(circularDeclaredPropertyAccess.ts, 28, 95))
180+
>_zod : Symbol(UnionSchema._zod, Decl(circularDeclaredPropertyAccess.ts, 38, 71))
181+
>UnionInternals : Symbol(UnionInternals, Decl(circularDeclaredPropertyAccess.ts, 34, 88))
182+
>T : Symbol(T, Decl(circularDeclaredPropertyAccess.ts, 38, 26))
183+
184+
type JsonValue = { [k: string]: JsonValue };
185+
>JsonValue : Symbol(JsonValue, Decl(circularDeclaredPropertyAccess.ts, 38, 97))
186+
>k : Symbol(k, Decl(circularDeclaredPropertyAccess.ts, 39, 24))
187+
>JsonValue : Symbol(JsonValue, Decl(circularDeclaredPropertyAccess.ts, 38, 97))
188+
189+
type _Json = UnionSchema<[RecordSchema<Json>]>;
190+
>_Json : Symbol(_Json, Decl(circularDeclaredPropertyAccess.ts, 39, 48))
191+
>UnionSchema : Symbol(UnionSchema, Decl(circularDeclaredPropertyAccess.ts, 37, 5))
192+
>RecordSchema : Symbol(RecordSchema, Decl(circularDeclaredPropertyAccess.ts, 33, 116))
193+
>Json : Symbol(Json, Decl(circularDeclaredPropertyAccess.ts, 42, 71))
194+
195+
type _JsonInternals = _Json["_zod"];
196+
>_JsonInternals : Symbol(_JsonInternals, Decl(circularDeclaredPropertyAccess.ts, 40, 51))
197+
>_Json : Symbol(_Json, Decl(circularDeclaredPropertyAccess.ts, 39, 48))
198+
199+
interface JsonInternals extends _JsonInternals { input: JsonValue }
200+
>JsonInternals : Symbol(JsonInternals, Decl(circularDeclaredPropertyAccess.ts, 41, 40))
201+
>_JsonInternals : Symbol(_JsonInternals, Decl(circularDeclaredPropertyAccess.ts, 40, 51))
202+
>input : Symbol(JsonInternals.input, Decl(circularDeclaredPropertyAccess.ts, 42, 52))
203+
>JsonValue : Symbol(JsonValue, Decl(circularDeclaredPropertyAccess.ts, 38, 97))
204+
205+
interface Json extends _Json { _zod: JsonInternals }
206+
>Json : Symbol(Json, Decl(circularDeclaredPropertyAccess.ts, 42, 71))
207+
>_Json : Symbol(_Json, Decl(circularDeclaredPropertyAccess.ts, 39, 48))
208+
>_zod : Symbol(Json._zod, Decl(circularDeclaredPropertyAccess.ts, 43, 34))
209+
>JsonInternals : Symbol(JsonInternals, Decl(circularDeclaredPropertyAccess.ts, 41, 40))
210+
}
211+

‎tsc/testdata/baselines/reference/compiler/circularDeclaredPropertyAccess.types‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,58 @@ interface D extends AB { kind: "a" }
5252

5353
interface E extends D {}
5454

55+
// Declared members are available while the base types are instantiated, which
56+
// can need one of them through a type argument referring back to the type.
57+
58+
namespace BaseTypeArguments {
59+
interface Internals<I = unknown> { input: I; optin?: "optional" | "defaulted" | undefined }
60+
>input : I
61+
>optin : "defaulted" | "optional" | undefined
62+
63+
interface Schema { _zod: Internals }
64+
>_zod : Internals<unknown>
65+
66+
type OptionalIn = { _zod: { optin: "optional" | "defaulted" } };
67+
>OptionalIn : OptionalIn
68+
>_zod : { optin: "optional" | "defaulted"; }
69+
>optin : "defaulted" | "optional"
70+
71+
type IsOptionalIn<T extends Schema> = T extends OptionalIn ? true : false;
72+
>IsOptionalIn : IsOptionalIn<T>
73+
>true : true
74+
>false : false
75+
76+
type RecordInput<V extends Schema> = [V] extends [OptionalIn] ? Partial<Record<string, V["_zod"]["input"]>> : Record<string, V["_zod"]["input"]>;
77+
>RecordInput : RecordInput<V>
78+
79+
interface RecordInternals<V extends Schema> extends Internals<RecordInput<V>> { optin?: "optional" | undefined }
80+
>optin : "optional" | undefined
81+
82+
interface RecordSchema<V extends Schema> extends Schema { _zod: RecordInternals<V> }
83+
>_zod : RecordInternals<V>
84+
85+
interface UnionInternals<T extends readonly Schema[]> extends Internals<T[number]["_zod"]["input"]> {
86+
optin: IsOptionalIn<T[number]> extends false ? "optional" | "defaulted" | undefined : "optional" | "defaulted";
87+
>optin : IsOptionalIn<T[number]> extends false ? "defaulted" | "optional" | undefined : "defaulted" | "optional"
88+
>false : false
89+
}
90+
interface UnionSchema<T extends readonly Schema[]> extends Schema { _zod: UnionInternals<T> }
91+
>_zod : UnionInternals<T>
92+
93+
type JsonValue = { [k: string]: JsonValue };
94+
>JsonValue : JsonValue
95+
>k : string
96+
97+
type _Json = UnionSchema<[RecordSchema<Json>]>;
98+
>_Json : _Json
99+
100+
type _JsonInternals = _Json["_zod"];
101+
>_JsonInternals : UnionInternals<[RecordSchema<Json>]>
102+
103+
interface JsonInternals extends _JsonInternals { input: JsonValue }
104+
>input : JsonValue
105+
106+
interface Json extends _Json { _zod: JsonInternals }
107+
>_zod : JsonInternals
108+
}
109+

‎tsc/testdata/tests/cases/compiler/circularDeclaredPropertyAccess.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,25 @@ type AB = A & B;
2323
interface D extends AB { kind: "a" }
2424

2525
interface E extends D {}
26+
27+
// Declared members are available while the base types are instantiated, which
28+
// can need one of them through a type argument referring back to the type.
29+
30+
namespace BaseTypeArguments {
31+
interface Internals<I = unknown> { input: I; optin?: "optional" | "defaulted" | undefined }
32+
interface Schema { _zod: Internals }
33+
type OptionalIn = { _zod: { optin: "optional" | "defaulted" } };
34+
type IsOptionalIn<T extends Schema> = T extends OptionalIn ? true : false;
35+
type RecordInput<V extends Schema> = [V] extends [OptionalIn] ? Partial<Record<string, V["_zod"]["input"]>> : Record<string, V["_zod"]["input"]>;
36+
interface RecordInternals<V extends Schema> extends Internals<RecordInput<V>> { optin?: "optional" | undefined }
37+
interface RecordSchema<V extends Schema> extends Schema { _zod: RecordInternals<V> }
38+
interface UnionInternals<T extends readonly Schema[]> extends Internals<T[number]["_zod"]["input"]> {
39+
optin: IsOptionalIn<T[number]> extends false ? "optional" | "defaulted" | undefined : "optional" | "defaulted";
40+
}
41+
interface UnionSchema<T extends readonly Schema[]> extends Schema { _zod: UnionInternals<T> }
42+
type JsonValue = { [k: string]: JsonValue };
43+
type _Json = UnionSchema<[RecordSchema<Json>]>;
44+
type _JsonInternals = _Json["_zod"];
45+
interface JsonInternals extends _JsonInternals { input: JsonValue }
46+
interface Json extends _Json { _zod: JsonInternals }
47+
}

0 commit comments

Comments
 (0)