From afdd2f28354ce8cea647279ed25d61e7b9946cf5 Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 28 Jun 2021 15:39:04 -0400 Subject: [PATCH] fix(compiler-sfc): support method signature in defineProps fix #2983 --- .../__snapshots__/compileScript.spec.ts.snap | 2 ++ .../__tests__/compileScript.spec.ts | 3 +++ packages/compiler-sfc/src/compileScript.ts | 21 ++++++++++++++----- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap index 9553ab12..2ff89f5f 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap @@ -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 interface: Test alias: Alias + method(): void union: string | number literalUnion: 'foo' | 'bar' diff --git a/packages/compiler-sfc/__tests__/compileScript.spec.ts b/packages/compiler-sfc/__tests__/compileScript.spec.ts index 6f91fa87..a33619dc 100644 --- a/packages/compiler-sfc/__tests__/compileScript.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScript.spec.ts @@ -532,6 +532,7 @@ const emit = defineEmits(['a', 'b']) recordRef: Record 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, diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index b7bb09de..c7d1667f 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -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`] } } }