From 839020a34062c84b8033900b0fc8d3e9abf7114a Mon Sep 17 00:00:00 2001 From: Disservin Date: Sun, 11 Aug 2024 00:07:08 +0200 Subject: [PATCH] fix(compiler-sfc): handle keyof operator with index object --- .../compileScript/resolveType.spec.ts | 59 +++++++++++++++++++ .../compiler-sfc/src/script/resolveType.ts | 2 +- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts b/packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts index b8b71878e03..ff33882568e 100644 --- a/packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts @@ -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( ` diff --git a/packages/compiler-sfc/src/script/resolveType.ts b/packages/compiler-sfc/src/script/resolveType.ts index 398d3ba9f42..c7880578e7a 100644 --- a/packages/compiler-sfc/src/script/resolveType.ts +++ b/packages/compiler-sfc/src/script/resolveType.ts @@ -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':