Преглед на файлове

fix(compiler-sfc): support type resolve for keyof for intersection & union types (#11132)

close #11129
Zhaolin Liang преди 1 година
родител
ревизия
495263a9cb
променени са 2 файла, в които са добавени 30 реда и са изтрити 4 реда
  1. 25 0
      packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts
  2. 5 4
      packages/compiler-sfc/src/script/resolveType.ts

+ 25 - 0
packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts

@@ -513,6 +513,31 @@ describe('resolveType', () => {
     })
     })
   })
   })
 
 
+  // #11129
+  test('keyof: intersection type', () => {
+    const { props } = resolve(`
+    type A = { name: string }
+    type B = A & { [key: number]: string }
+    defineProps<{
+      foo: keyof B
+    }>()`)
+    expect(props).toStrictEqual({
+      foo: ['String', 'Number'],
+    })
+  })
+
+  test('keyof: union type', () => {
+    const { props } = resolve(`
+    type A = { name: string }
+    type B = A | { [key: number]: string }
+    defineProps<{
+      foo: keyof B
+    }>()`)
+    expect(props).toStrictEqual({
+      foo: ['String', 'Number'],
+    })
+  })
+
   test('keyof: utility type', () => {
   test('keyof: utility type', () => {
     const { props } = resolve(
     const { props } = resolve(
       `
       `

+ 5 - 4
packages/compiler-sfc/src/script/resolveType.ts

@@ -1686,9 +1686,9 @@ export function inferRuntimeType(
         return inferRuntimeType(ctx, node.typeAnnotation, scope)
         return inferRuntimeType(ctx, node.typeAnnotation, scope)
 
 
       case 'TSUnionType':
       case 'TSUnionType':
-        return flattenTypes(ctx, node.types, scope)
+        return flattenTypes(ctx, node.types, scope, isKeyOf)
       case 'TSIntersectionType': {
       case 'TSIntersectionType': {
-        return flattenTypes(ctx, node.types, scope).filter(
+        return flattenTypes(ctx, node.types, scope, isKeyOf).filter(
           t => t !== UNKNOWN_TYPE,
           t => t !== UNKNOWN_TYPE,
         )
         )
       }
       }
@@ -1760,14 +1760,15 @@ function flattenTypes(
   ctx: TypeResolveContext,
   ctx: TypeResolveContext,
   types: TSType[],
   types: TSType[],
   scope: TypeScope,
   scope: TypeScope,
+  isKeyOf: boolean = false,
 ): string[] {
 ): string[] {
   if (types.length === 1) {
   if (types.length === 1) {
-    return inferRuntimeType(ctx, types[0], scope)
+    return inferRuntimeType(ctx, types[0], scope, isKeyOf)
   }
   }
   return [
   return [
     ...new Set(
     ...new Set(
       ([] as string[]).concat(
       ([] as string[]).concat(
-        ...types.map(t => inferRuntimeType(ctx, t, scope)),
+        ...types.map(t => inferRuntimeType(ctx, t, scope, isKeyOf)),
       ),
       ),
     ),
     ),
   ]
   ]