@@ -140,9 +140,9 @@ function innerResolveTypeElements(
140
140
) : ResolvedElements {
141
141
switch ( node . type ) {
142
142
case 'TSTypeLiteral' :
143
- return typeElementsToMap ( ctx , node . members , scope )
143
+ return typeElementsToMap ( ctx , node . members , scope , typeParameters )
144
144
case 'TSInterfaceDeclaration' :
145
- return resolveInterfaceMembers ( ctx , node , scope )
145
+ return resolveInterfaceMembers ( ctx , node , scope , typeParameters )
146
146
case 'TSTypeAliasDeclaration' :
147
147
case 'TSParenthesizedType' :
148
148
return resolveTypeElements (
@@ -156,20 +156,8 @@ function innerResolveTypeElements(
156
156
}
157
157
case 'TSUnionType' :
158
158
case 'TSIntersectionType' :
159
- const types = typeParameters
160
- ? node . types . map ( t => {
161
- if (
162
- t . type === 'TSTypeReference' &&
163
- t . typeName . type === 'Identifier' &&
164
- typeParameters [ t . typeName . name ]
165
- ) {
166
- return typeParameters [ t . typeName . name ]
167
- }
168
- return t
169
- } )
170
- : node . types
171
159
return mergeElements (
172
- types . map ( t => resolveTypeElements ( ctx , t , scope ) ) ,
160
+ node . types . map ( t => resolveTypeElements ( ctx , t , scope , typeParameters ) ) ,
173
161
node . type
174
162
)
175
163
case 'TSMappedType' :
@@ -191,15 +179,21 @@ function innerResolveTypeElements(
191
179
scope . imports [ typeName ] ?. source === 'vue'
192
180
) {
193
181
return resolveExtractPropTypes (
194
- resolveTypeElements ( ctx , node . typeParameters . params [ 0 ] , scope ) ,
182
+ resolveTypeElements (
183
+ ctx ,
184
+ node . typeParameters . params [ 0 ] ,
185
+ scope ,
186
+ typeParameters
187
+ ) ,
195
188
scope
196
189
)
197
190
}
198
191
const resolved = resolveTypeReference ( ctx , node , scope )
199
192
if ( resolved ) {
200
193
const typeParams : Record < string , Node > = Object . create ( null )
201
194
if (
202
- resolved . type === 'TSTypeAliasDeclaration' &&
195
+ ( resolved . type === 'TSTypeAliasDeclaration' ||
196
+ resolved . type === 'TSInterfaceDeclaration' ) &&
203
197
resolved . typeParameters &&
204
198
node . typeParameters
205
199
) {
@@ -294,11 +288,17 @@ function innerResolveTypeElements(
294
288
function typeElementsToMap (
295
289
ctx : TypeResolveContext ,
296
290
elements : TSTypeElement [ ] ,
297
- scope = ctxToScope ( ctx )
291
+ scope = ctxToScope ( ctx ) ,
292
+ typeParameters ?: Record < string , Node >
298
293
) : ResolvedElements {
299
294
const res : ResolvedElements = { props : { } }
300
295
for ( const e of elements ) {
301
296
if ( e . type === 'TSPropertySignature' || e . type === 'TSMethodSignature' ) {
297
+ // capture generic parameters on node's scope
298
+ if ( typeParameters ) {
299
+ scope = createChildScope ( scope )
300
+ Object . assign ( scope . types , typeParameters )
301
+ }
302
302
; ( e as MaybeWithScope ) . _ownerScope = scope
303
303
const name = getId ( e . key )
304
304
if ( name && ! e . computed ) {
@@ -374,9 +374,15 @@ function createProperty(
374
374
function resolveInterfaceMembers (
375
375
ctx : TypeResolveContext ,
376
376
node : TSInterfaceDeclaration & MaybeWithScope ,
377
- scope : TypeScope
377
+ scope : TypeScope ,
378
+ typeParameters ?: Record < string , Node >
378
379
) : ResolvedElements {
379
- const base = typeElementsToMap ( ctx , node . body . body , node . _ownerScope )
380
+ const base = typeElementsToMap (
381
+ ctx ,
382
+ node . body . body ,
383
+ node . _ownerScope ,
384
+ typeParameters
385
+ )
380
386
if ( node . extends ) {
381
387
for ( const ext of node . extends ) {
382
388
if (
@@ -1160,14 +1166,7 @@ function moduleDeclToScope(
1160
1166
return node . _resolvedChildScope
1161
1167
}
1162
1168
1163
- const scope = new TypeScope (
1164
- parentScope . filename ,
1165
- parentScope . source ,
1166
- parentScope . offset ,
1167
- Object . create ( parentScope . imports ) ,
1168
- Object . create ( parentScope . types ) ,
1169
- Object . create ( parentScope . declares )
1170
- )
1169
+ const scope = createChildScope ( parentScope )
1171
1170
1172
1171
if ( node . body . type === 'TSModuleDeclaration' ) {
1173
1172
const decl = node . body as TSModuleDeclaration & WithScope
@@ -1181,6 +1180,17 @@ function moduleDeclToScope(
1181
1180
return ( node . _resolvedChildScope = scope )
1182
1181
}
1183
1182
1183
+ function createChildScope ( parentScope : TypeScope ) {
1184
+ return new TypeScope (
1185
+ parentScope . filename ,
1186
+ parentScope . source ,
1187
+ parentScope . offset ,
1188
+ Object . create ( parentScope . imports ) ,
1189
+ Object . create ( parentScope . types ) ,
1190
+ Object . create ( parentScope . declares )
1191
+ )
1192
+ }
1193
+
1184
1194
const importExportRE = / ^ I m p o r t | ^ E x p o r t /
1185
1195
1186
1196
function recordTypes (
0 commit comments