Skip to content

Commit 8df75d6

Browse files
committed
fix(compiler-sfc): enhance inferRuntimeType to support TSMappedType with indexed access
only for the strict pattern {[K in keyof T]: T[K]} close #13847
1 parent 75220c7 commit 8df75d6

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,6 +1781,47 @@ export function inferRuntimeType(
17811781
typeParameters,
17821782
).filter(t => t !== UNKNOWN_TYPE)
17831783
}
1784+
case 'TSMappedType': {
1785+
// only support { [K in keyof T]: T[K] }
1786+
const { typeAnnotation, typeParameter } = node
1787+
if (
1788+
typeAnnotation &&
1789+
typeAnnotation.type === 'TSIndexedAccessType' &&
1790+
typeParameter &&
1791+
typeParameter.constraint &&
1792+
typeParameters
1793+
) {
1794+
const constraint = typeParameter.constraint
1795+
if (
1796+
constraint.type === 'TSTypeOperator' &&
1797+
constraint.operator === 'keyof' &&
1798+
constraint.typeAnnotation &&
1799+
constraint.typeAnnotation.type === 'TSTypeReference' &&
1800+
constraint.typeAnnotation.typeName.type === 'Identifier'
1801+
) {
1802+
const typeName = constraint.typeAnnotation.typeName.name
1803+
const index = typeAnnotation.indexType
1804+
const obj = typeAnnotation.objectType
1805+
if (
1806+
obj &&
1807+
obj.type === 'TSTypeReference' &&
1808+
obj.typeName.type === 'Identifier' &&
1809+
obj.typeName.name === typeName &&
1810+
index &&
1811+
index.type === 'TSTypeReference' &&
1812+
index.typeName.type === 'Identifier' &&
1813+
index.typeName.name === typeParameter.name
1814+
) {
1815+
const targetType = typeParameters[typeName]
1816+
if (targetType) {
1817+
return inferRuntimeType(ctx, targetType, scope)
1818+
}
1819+
}
1820+
}
1821+
}
1822+
1823+
return [UNKNOWN_TYPE]
1824+
}
17841825

17851826
case 'TSEnumDeclaration':
17861827
return inferEnumType(node)

0 commit comments

Comments
 (0)