Skip to content

Commit 9c42a1e

Browse files
authored
fix(compiler-sfc): add type for props include Function in prod mode (vuejs#4938)
1 parent f454dd6 commit 9c42a1e

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

packages/compiler-sfc/__tests__/__snapshots__/compileScriptPropsTransform.spec.ts.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ export default /*#__PURE__*/_defineComponent({
8484
bar: { default: () => {} },
8585
baz: null,
8686
boola: { type: Boolean },
87-
boolb: { type: [Boolean, Number] }
87+
boolb: { type: [Boolean, Number] },
88+
func: { type: Function, default: () => () => {} }
8889
},
8990
setup(__props: any) {
9091

packages/compiler-sfc/__tests__/compileScriptPropsTransform.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ describe('sfc props transform', () => {
8383
const { content } = compile(
8484
`
8585
<script setup lang="ts">
86-
const { foo = 1, bar = {} } = defineProps<{ foo?: number, bar?: object, baz?: any, boola?: boolean, boolb?: boolean | number }>()
86+
const { foo = 1, bar = {}, func = () => {} } = defineProps<{ foo?: number, bar?: object, baz?: any, boola?: boolean, boolb?: boolean | number, func?: Function }>()
8787
</script>
8888
`,
8989
{ isProd: true }
@@ -95,7 +95,8 @@ describe('sfc props transform', () => {
9595
bar: { default: () => {} },
9696
baz: null,
9797
boola: { type: Boolean },
98-
boolb: { type: [Boolean, Number] }
98+
boolb: { type: [Boolean, Number] },
99+
func: { type: Function, default: () => () => {} }
99100
}`)
100101
assertCode(content)
101102
})

packages/compiler-sfc/src/compileScript.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -692,11 +692,13 @@ export function compileScript(
692692
)}, required: ${required}${
693693
defaultString ? `, ${defaultString}` : ``
694694
} }`
695-
} else if (type.indexOf('Boolean') > -1) {
696-
// production: if boolean exists, should keep the type.
697-
return `${key}: { type: ${toRuntimeTypeString(
698-
type
699-
)}${
695+
} else if (
696+
type.some(
697+
el => el === 'Boolean' || (defaultString && el === 'Function')
698+
)
699+
) {
700+
// #4783 production: if boolean or defaultString and function exists, should keep the type.
701+
return `${key}: { type: ${toRuntimeTypeString(type)}${
700702
defaultString ? `, ${defaultString}` : ``
701703
} }`
702704
} else {
@@ -1631,10 +1633,7 @@ function extractRuntimeProps(
16311633
if (m.type === 'TSMethodSignature') {
16321634
type = ['Function']
16331635
} else if (m.typeAnnotation) {
1634-
type = inferRuntimeType(
1635-
m.typeAnnotation.typeAnnotation,
1636-
declaredTypes
1637-
)
1636+
type = inferRuntimeType(m.typeAnnotation.typeAnnotation, declaredTypes)
16381637
}
16391638
props[m.key.name] = {
16401639
key: m.key.name,

0 commit comments

Comments
 (0)