fix(compiler-sfc): add type for props include Function in prod mode (#4938)
This commit is contained in:
parent
f454dd62ab
commit
9c42a1e2a3
@ -84,7 +84,8 @@ export default /*#__PURE__*/_defineComponent({
|
|||||||
bar: { default: () => {} },
|
bar: { default: () => {} },
|
||||||
baz: null,
|
baz: null,
|
||||||
boola: { type: Boolean },
|
boola: { type: Boolean },
|
||||||
boolb: { type: [Boolean, Number] }
|
boolb: { type: [Boolean, Number] },
|
||||||
|
func: { type: Function, default: () => () => {} }
|
||||||
},
|
},
|
||||||
setup(__props: any) {
|
setup(__props: any) {
|
||||||
|
|
||||||
|
@ -83,7 +83,7 @@ describe('sfc props transform', () => {
|
|||||||
const { content } = compile(
|
const { content } = compile(
|
||||||
`
|
`
|
||||||
<script setup lang="ts">
|
<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>
|
</script>
|
||||||
`,
|
`,
|
||||||
{ isProd: true }
|
{ isProd: true }
|
||||||
@ -95,7 +95,8 @@ describe('sfc props transform', () => {
|
|||||||
bar: { default: () => {} },
|
bar: { default: () => {} },
|
||||||
baz: null,
|
baz: null,
|
||||||
boola: { type: Boolean },
|
boola: { type: Boolean },
|
||||||
boolb: { type: [Boolean, Number] }
|
boolb: { type: [Boolean, Number] },
|
||||||
|
func: { type: Function, default: () => () => {} }
|
||||||
}`)
|
}`)
|
||||||
assertCode(content)
|
assertCode(content)
|
||||||
})
|
})
|
||||||
|
@ -692,11 +692,13 @@ export function compileScript(
|
|||||||
)}, required: ${required}${
|
)}, required: ${required}${
|
||||||
defaultString ? `, ${defaultString}` : ``
|
defaultString ? `, ${defaultString}` : ``
|
||||||
} }`
|
} }`
|
||||||
} else if (type.indexOf('Boolean') > -1) {
|
} else if (
|
||||||
// production: if boolean exists, should keep the type.
|
type.some(
|
||||||
return `${key}: { type: ${toRuntimeTypeString(
|
el => el === 'Boolean' || (defaultString && el === 'Function')
|
||||||
type
|
)
|
||||||
)}${
|
) {
|
||||||
|
// #4783 production: if boolean or defaultString and function exists, should keep the type.
|
||||||
|
return `${key}: { type: ${toRuntimeTypeString(type)}${
|
||||||
defaultString ? `, ${defaultString}` : ``
|
defaultString ? `, ${defaultString}` : ``
|
||||||
} }`
|
} }`
|
||||||
} else {
|
} else {
|
||||||
@ -1631,10 +1633,7 @@ function extractRuntimeProps(
|
|||||||
if (m.type === 'TSMethodSignature') {
|
if (m.type === 'TSMethodSignature') {
|
||||||
type = ['Function']
|
type = ['Function']
|
||||||
} else if (m.typeAnnotation) {
|
} else if (m.typeAnnotation) {
|
||||||
type = inferRuntimeType(
|
type = inferRuntimeType(m.typeAnnotation.typeAnnotation, declaredTypes)
|
||||||
m.typeAnnotation.typeAnnotation,
|
|
||||||
declaredTypes
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
props[m.key.name] = {
|
props[m.key.name] = {
|
||||||
key: m.key.name,
|
key: m.key.name,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user