From 9c42a1e2a3385f3b33faed5cdcc430bf8c1fc4b2 Mon Sep 17 00:00:00 2001 From: ygj6 <7699524+ygj6@users.noreply.github.com> Date: Mon, 15 Nov 2021 10:31:11 +0800 Subject: [PATCH] fix(compiler-sfc): add type for props include Function in prod mode (#4938) --- .../compileScriptPropsTransform.spec.ts.snap | 3 ++- .../compileScriptPropsTransform.spec.ts | 5 +++-- packages/compiler-sfc/src/compileScript.ts | 17 ++++++++--------- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScriptPropsTransform.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScriptPropsTransform.spec.ts.snap index 5b11ae5c..31378a26 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScriptPropsTransform.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScriptPropsTransform.spec.ts.snap @@ -84,7 +84,8 @@ export default /*#__PURE__*/_defineComponent({ bar: { default: () => {} }, baz: null, boola: { type: Boolean }, - boolb: { type: [Boolean, Number] } + boolb: { type: [Boolean, Number] }, + func: { type: Function, default: () => () => {} } }, setup(__props: any) { diff --git a/packages/compiler-sfc/__tests__/compileScriptPropsTransform.spec.ts b/packages/compiler-sfc/__tests__/compileScriptPropsTransform.spec.ts index 5d406608..b71495d7 100644 --- a/packages/compiler-sfc/__tests__/compileScriptPropsTransform.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScriptPropsTransform.spec.ts @@ -83,7 +83,7 @@ describe('sfc props transform', () => { const { content } = compile( ` `, { isProd: true } @@ -95,7 +95,8 @@ describe('sfc props transform', () => { bar: { default: () => {} }, baz: null, boola: { type: Boolean }, - boolb: { type: [Boolean, Number] } + boolb: { type: [Boolean, Number] }, + func: { type: Function, default: () => () => {} } }`) assertCode(content) }) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index dcaf3848..fe1ae0ff 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -692,11 +692,13 @@ export function compileScript( )}, required: ${required}${ defaultString ? `, ${defaultString}` : `` } }` - } else if (type.indexOf('Boolean') > -1) { - // production: if boolean exists, should keep the type. - return `${key}: { type: ${toRuntimeTypeString( - type - )}${ + } else if ( + type.some( + el => el === 'Boolean' || (defaultString && el === 'Function') + ) + ) { + // #4783 production: if boolean or defaultString and function exists, should keep the type. + return `${key}: { type: ${toRuntimeTypeString(type)}${ defaultString ? `, ${defaultString}` : `` } }` } else { @@ -1631,10 +1633,7 @@ function extractRuntimeProps( if (m.type === 'TSMethodSignature') { type = ['Function'] } else if (m.typeAnnotation) { - type = inferRuntimeType( - m.typeAnnotation.typeAnnotation, - declaredTypes - ) + type = inferRuntimeType(m.typeAnnotation.typeAnnotation, declaredTypes) } props[m.key.name] = { key: m.key.name,