Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-sfc): add type for props include Function in prod mode #4938

Merged
merged 2 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
11 changes: 3 additions & 8 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimization: when type is Function, prod type is only needed if default is also used.

// production: if boolean exists, should keep the type.
return `${key}: { type: ${toRuntimeTypeString(
type
)}${
return `${key}: { type: ${toRuntimeTypeString(type)}${
defaultString ? `, ${defaultString}` : ``
} }`
} else {
Expand Down Expand Up @@ -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,
Expand Down