Skip to content

Commit fe00815

Browse files
authored
fix(compiler-sfc): handle keyof operator with index object (#11581)
1 parent e9e0815 commit fe00815

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

Diff for: packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts

+59
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,65 @@ describe('resolveType', () => {
596596
})
597597
})
598598

599+
test('keyof: nested object with number', () => {
600+
const { props } = resolve(
601+
`
602+
interface Type {
603+
deep: {
604+
1: any
605+
}
606+
}
607+
608+
defineProps<{
609+
route: keyof Type['deep']
610+
}>()`,
611+
)
612+
613+
expect(props).toStrictEqual({
614+
route: ['Number'],
615+
})
616+
})
617+
618+
test('keyof: nested object with string', () => {
619+
const { props } = resolve(
620+
`
621+
interface Type {
622+
deep: {
623+
foo: any
624+
}
625+
}
626+
627+
defineProps<{
628+
route: keyof Type['deep']
629+
}>()`,
630+
)
631+
632+
expect(props).toStrictEqual({
633+
route: ['String'],
634+
})
635+
})
636+
637+
test('keyof: nested object with intermediate', () => {
638+
const { props } = resolve(
639+
`
640+
interface Type {
641+
deep: {
642+
foo: any
643+
}
644+
}
645+
646+
type Foo = Type['deep']
647+
648+
defineProps<{
649+
route: keyof Foo
650+
}>()`,
651+
)
652+
653+
expect(props).toStrictEqual({
654+
route: ['String'],
655+
})
656+
})
657+
599658
test('ExtractPropTypes (element-plus)', () => {
600659
const { props, raw } = resolve(
601660
`

Diff for: packages/compiler-sfc/src/script/resolveType.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1703,7 +1703,7 @@ export function inferRuntimeType(
17031703

17041704
case 'TSIndexedAccessType': {
17051705
const types = resolveIndexType(ctx, node, scope)
1706-
return flattenTypes(ctx, types, scope)
1706+
return flattenTypes(ctx, types, scope, isKeyOf)
17071707
}
17081708

17091709
case 'ClassDeclaration':

0 commit comments

Comments
 (0)