Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-sfc): handle keyof operator with indexed object #11581

Merged
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
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