Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,22 @@ describe('resolveType', () => {
})
})

test('TSMappedType with indexed access', () => {
const { props } = resolve(
`
type Prettify<T> = { [K in keyof T]: T[K] } & {}
type Side = 'top' | 'right' | 'bottom' | 'left'
type AlignedPlacement = \`\${Side}-\${Alignment}\`
type Alignment = 'start' | 'end'
type Placement = Prettify<Side | AlignedPlacement>
defineProps<{placement?: Placement}>()
`,
)
expect(props).toStrictEqual({
placement: ['String', 'Object'],
})
})

describe('type alias declaration', () => {
// #13240
test('function type', () => {
Expand Down
41 changes: 41 additions & 0 deletions packages/compiler-sfc/src/script/resolveType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1781,6 +1781,47 @@ export function inferRuntimeType(
typeParameters,
).filter(t => t !== UNKNOWN_TYPE)
}
case 'TSMappedType': {
// only support { [K in keyof T]: T[K] }
const { typeAnnotation, typeParameter } = node
if (
typeAnnotation &&
typeAnnotation.type === 'TSIndexedAccessType' &&
typeParameter &&
typeParameter.constraint &&
typeParameters
) {
const constraint = typeParameter.constraint
if (
constraint.type === 'TSTypeOperator' &&
constraint.operator === 'keyof' &&
constraint.typeAnnotation &&
constraint.typeAnnotation.type === 'TSTypeReference' &&
constraint.typeAnnotation.typeName.type === 'Identifier'
) {
const typeName = constraint.typeAnnotation.typeName.name
const index = typeAnnotation.indexType
const obj = typeAnnotation.objectType
if (
obj &&
obj.type === 'TSTypeReference' &&
obj.typeName.type === 'Identifier' &&
obj.typeName.name === typeName &&
index &&
index.type === 'TSTypeReference' &&
index.typeName.type === 'Identifier' &&
index.typeName.name === typeParameter.name
) {
const targetType = typeParameters[typeName]
if (targetType) {
return inferRuntimeType(ctx, targetType, scope)
}
}
}
}

return [UNKNOWN_TYPE]
}

case 'TSEnumDeclaration':
return inferEnumType(node)
Expand Down
Loading