]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): support method signature in defineProps
authorEvan You <yyx990803@gmail.com>
Mon, 28 Jun 2021 19:39:04 +0000 (15:39 -0400)
committerEvan You <yyx990803@gmail.com>
Mon, 28 Jun 2021 19:39:04 +0000 (15:39 -0400)
fix #2983

packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap
packages/compiler-sfc/__tests__/compileScript.spec.ts
packages/compiler-sfc/src/compileScript.ts

index 9553ab1250e8f5609a4261726b3a3b756d86af23..2ff89f5ff66a3cfb70c547a877c2e7560f030c20 100644 (file)
@@ -864,6 +864,7 @@ export default _defineComponent({
     recordRef: { type: Object, required: true },
     interface: { type: Object, required: true },
     alias: { type: Array, required: true },
+    method: { type: Function, required: true },
     union: { type: [String, Number], required: true },
     literalUnion: { type: [String, String], required: true },
     literalUnionMixed: { type: [String, Number, Boolean], required: true },
@@ -887,6 +888,7 @@ export default _defineComponent({
         recordRef: Record<string, null>
         interface: Test
         alias: Alias
+        method(): void
 
         union: string | number
         literalUnion: 'foo' | 'bar'
index 6f91fa872385bea5df6cdc9fc733befa7d0a1346..a33619dc3a3d3279b528d24b33a5ac1b4d2e07af 100644 (file)
@@ -532,6 +532,7 @@ const emit = defineEmits(['a', 'b'])
         recordRef: Record<string, null>
         interface: Test
         alias: Alias
+        method(): void
 
         union: string | number
         literalUnion: 'foo' | 'bar'
@@ -557,6 +558,7 @@ const emit = defineEmits(['a', 'b'])
       expect(content).toMatch(`recordRef: { type: Object, required: true }`)
       expect(content).toMatch(`interface: { type: Object, required: true }`)
       expect(content).toMatch(`alias: { type: Array, required: true }`)
+      expect(content).toMatch(`method: { type: Function, required: true }`)
       expect(content).toMatch(
         `union: { type: [String, Number], required: true }`
       )
@@ -585,6 +587,7 @@ const emit = defineEmits(['a', 'b'])
         recordRef: BindingTypes.PROPS,
         interface: BindingTypes.PROPS,
         alias: BindingTypes.PROPS,
+        method: BindingTypes.PROPS,
         union: BindingTypes.PROPS,
         literalUnion: BindingTypes.PROPS,
         literalUnionMixed: BindingTypes.PROPS,
index b7bb09de36484ace63876c7b7984f9948c3bac94..c7d1667f7a63a6efcf87e67fde7b45739e64de2f 100644 (file)
@@ -1355,14 +1355,25 @@ function extractRuntimeProps(
 ) {
   const members = node.type === 'TSTypeLiteral' ? node.members : node.body
   for (const m of members) {
-    if (m.type === 'TSPropertySignature' && m.key.type === 'Identifier') {
+    if (
+      (m.type === 'TSPropertySignature' || m.type === 'TSMethodSignature') &&
+      m.key.type === 'Identifier'
+    ) {
+      let type
+      if (__DEV__) {
+        if (m.type === 'TSMethodSignature') {
+          type = ['Function']
+        } else if (m.typeAnnotation) {
+          type = inferRuntimeType(
+            m.typeAnnotation.typeAnnotation,
+            declaredTypes
+          )
+        }
+      }
       props[m.key.name] = {
         key: m.key.name,
         required: !m.optional,
-        type:
-          __DEV__ && m.typeAnnotation
-            ? inferRuntimeType(m.typeAnnotation.typeAnnotation, declaredTypes)
-            : [`null`]
+        type: type || [`null`]
       }
     }
   }