From 8e3ea786629703f13750e28251fcef1d0f484538 Mon Sep 17 00:00:00 2001 From: ygj6 Date: Fri, 12 Nov 2021 09:53:49 +0800 Subject: [PATCH 1/2] fix(compiler-sfc): add type for props include Function in prod mode --- .../compileScriptPropsTransform.spec.ts.snap | 3 ++- .../__tests__/compileScriptPropsTransform.spec.ts | 5 +++-- packages/compiler-sfc/src/compileScript.ts | 11 +++-------- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScriptPropsTransform.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScriptPropsTransform.spec.ts.snap index 5b11ae5c1a5..31378a26535 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 5d406608288..b71495d726c 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 dcaf3848caa..de6b37b7e87 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -692,11 +692,9 @@ export function compileScript( )}, required: ${required}${ defaultString ? `, ${defaultString}` : `` } }` - } else if (type.indexOf('Boolean') > -1) { + } else if (type.some(el => el === 'Boolean' || el === 'Function')) { // production: if boolean exists, should keep the type. - return `${key}: { type: ${toRuntimeTypeString( - type - )}${ + return `${key}: { type: ${toRuntimeTypeString(type)}${ defaultString ? `, ${defaultString}` : `` } }` } else { @@ -1631,10 +1629,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, From 9e70205548c52c55a0a1f65d2ce998cdb23eec81 Mon Sep 17 00:00:00 2001 From: ygj6 Date: Fri, 12 Nov 2021 14:46:49 +0800 Subject: [PATCH 2/2] fix(compiler-sfc): when type is Function, prod type is only needed if default is also used --- packages/compiler-sfc/src/compileScript.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index de6b37b7e87..fe1ae0ff0e8 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -692,8 +692,12 @@ export function compileScript( )}, required: ${required}${ defaultString ? `, ${defaultString}` : `` } }` - } else if (type.some(el => el === 'Boolean' || el === 'Function')) { - // production: if boolean exists, should keep the 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}` : `` } }`