Skip to content

Commit

Permalink
fix(compiler-sfc): add type for props include Function in prod mode (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ygj6 committed Nov 15, 2021
1 parent f454dd6 commit 9c42a1e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
Expand Up @@ -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) {
Expand Down
Expand Up @@ -83,7 +83,7 @@ describe('sfc props transform', () => {
const { content } = compile(
`
<script setup lang="ts">
const { foo = 1, bar = {} } = defineProps<{ foo?: number, bar?: object, baz?: any, boola?: boolean, boolb?: boolean | number }>()
const { foo = 1, bar = {}, func = () => {} } = defineProps<{ foo?: number, bar?: object, baz?: any, boola?: boolean, boolb?: boolean | number, func?: Function }>()
</script>
`,
{ isProd: true }
Expand All @@ -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)
})
Expand Down
17 changes: 8 additions & 9 deletions packages/compiler-sfc/src/compileScript.ts
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 9c42a1e

Please sign in to comment.