Skip to content

Commit

Permalink
fix(compiler-sfc): handle keyof operator with index object (#11581)
Browse files Browse the repository at this point in the history
  • Loading branch information
Disservin authored Aug 15, 2024
1 parent e9e0815 commit fe00815
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
59 changes: 59 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,65 @@ describe('resolveType', () => {
})
})

test('keyof: nested object with number', () => {
const { props } = resolve(
`
interface Type {
deep: {
1: any
}
}
defineProps<{
route: keyof Type['deep']
}>()`,
)

expect(props).toStrictEqual({
route: ['Number'],
})
})

test('keyof: nested object with string', () => {
const { props } = resolve(
`
interface Type {
deep: {
foo: any
}
}
defineProps<{
route: keyof Type['deep']
}>()`,
)

expect(props).toStrictEqual({
route: ['String'],
})
})

test('keyof: nested object with intermediate', () => {
const { props } = resolve(
`
interface Type {
deep: {
foo: any
}
}
type Foo = Type['deep']
defineProps<{
route: keyof Foo
}>()`,
)

expect(props).toStrictEqual({
route: ['String'],
})
})

test('ExtractPropTypes (element-plus)', () => {
const { props, raw } = resolve(
`
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-sfc/src/script/resolveType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1703,7 +1703,7 @@ export function inferRuntimeType(

case 'TSIndexedAccessType': {
const types = resolveIndexType(ctx, node, scope)
return flattenTypes(ctx, types, scope)
return flattenTypes(ctx, types, scope, isKeyOf)
}

case 'ClassDeclaration':
Expand Down

0 comments on commit fe00815

Please sign in to comment.