Skip to content

Commit a8d0b1b

Browse files
committed
fix(compiler-sfc): fix type resolution for shared type w/ different generic parameters
close #9871
1 parent 1b522ca commit a8d0b1b

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts

+34-3
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,34 @@ describe('resolveType', () => {
939939
manufacturer: ['Object']
940940
})
941941
})
942+
943+
// #9871
944+
test('shared generics with different args', () => {
945+
const files = {
946+
'/foo.ts': `export interface Foo<T> { value: T }`
947+
}
948+
const { props } = resolve(
949+
`import type { Foo } from './foo'
950+
defineProps<Foo<string>>()`,
951+
files,
952+
undefined,
953+
`/One.vue`
954+
)
955+
expect(props).toStrictEqual({
956+
value: ['String']
957+
})
958+
const { props: props2 } = resolve(
959+
`import type { Foo } from './foo'
960+
defineProps<Foo<number>>()`,
961+
files,
962+
undefined,
963+
`/Two.vue`,
964+
false /* do not invalidate cache */
965+
)
966+
expect(props2).toStrictEqual({
967+
value: ['Number']
968+
})
969+
})
942970
})
943971

944972
describe('errors', () => {
@@ -1012,7 +1040,8 @@ function resolve(
10121040
code: string,
10131041
files: Record<string, string> = {},
10141042
options?: Partial<SFCScriptCompileOptions>,
1015-
sourceFileName: string = '/Test.vue'
1043+
sourceFileName: string = '/Test.vue',
1044+
invalidateCache = true
10161045
) {
10171046
const { descriptor } = parse(`<script setup lang="ts">\n${code}\n</script>`, {
10181047
filename: sourceFileName
@@ -1030,8 +1059,10 @@ function resolve(
10301059
...options
10311060
})
10321061

1033-
for (const file in files) {
1034-
invalidateTypeCache(file)
1062+
if (invalidateCache) {
1063+
for (const file in files) {
1064+
invalidateTypeCache(file)
1065+
}
10351066
}
10361067

10371068
// ctx.userImports is collected when calling compileScript(), but we are

packages/compiler-sfc/src/script/resolveType.ts

+15-9
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export class TypeScope {
9090
public types: Record<string, ScopeTypeNode> = Object.create(null),
9191
public declares: Record<string, ScopeTypeNode> = Object.create(null)
9292
) {}
93-
93+
isGenericScope = false
9494
resolvedImportSources: Record<string, string> = Object.create(null)
9595
exportedTypes: Record<string, ScopeTypeNode> = Object.create(null)
9696
exportedDeclares: Record<string, ScopeTypeNode> = Object.create(null)
@@ -121,15 +121,17 @@ export function resolveTypeElements(
121121
scope?: TypeScope,
122122
typeParameters?: Record<string, Node>
123123
): ResolvedElements {
124-
if (node._resolvedElements) {
124+
const canCache = !typeParameters
125+
if (canCache && node._resolvedElements) {
125126
return node._resolvedElements
126127
}
127-
return (node._resolvedElements = innerResolveTypeElements(
128+
const resolved = innerResolveTypeElements(
128129
ctx,
129130
node,
130131
node._ownerScope || scope || ctxToScope(ctx),
131132
typeParameters
132-
))
133+
)
134+
return canCache ? (node._resolvedElements = resolved) : resolved
133135
}
134136

135137
function innerResolveTypeElements(
@@ -190,17 +192,18 @@ function innerResolveTypeElements(
190192
}
191193
const resolved = resolveTypeReference(ctx, node, scope)
192194
if (resolved) {
193-
const typeParams: Record<string, Node> = Object.create(null)
195+
let typeParams: Record<string, Node> | undefined
194196
if (
195197
(resolved.type === 'TSTypeAliasDeclaration' ||
196198
resolved.type === 'TSInterfaceDeclaration') &&
197199
resolved.typeParameters &&
198200
node.typeParameters
199201
) {
202+
typeParams = Object.create(null)
200203
resolved.typeParameters.params.forEach((p, i) => {
201204
let param = typeParameters && typeParameters[p.name]
202205
if (!param) param = node.typeParameters!.params[i]
203-
typeParams[p.name] = param
206+
typeParams![p.name] = param
204207
})
205208
}
206209
return resolveTypeElements(
@@ -297,6 +300,7 @@ function typeElementsToMap(
297300
// capture generic parameters on node's scope
298301
if (typeParameters) {
299302
scope = createChildScope(scope)
303+
scope.isGenericScope = true
300304
Object.assign(scope.types, typeParameters)
301305
}
302306
;(e as MaybeWithScope)._ownerScope = scope
@@ -669,16 +673,18 @@ function resolveTypeReference(
669673
name?: string,
670674
onlyExported = false
671675
): ScopeTypeNode | undefined {
672-
if (node._resolvedReference) {
676+
const canCache = !scope?.isGenericScope
677+
if (canCache && node._resolvedReference) {
673678
return node._resolvedReference
674679
}
675-
return (node._resolvedReference = innerResolveTypeReference(
680+
const resolved = innerResolveTypeReference(
676681
ctx,
677682
scope || ctxToScope(ctx),
678683
name || getReferenceName(node),
679684
node,
680685
onlyExported
681-
))
686+
)
687+
return canCache ? (node._resolvedReference = resolved) : resolved
682688
}
683689

684690
function innerResolveTypeReference(

0 commit comments

Comments
 (0)