Skip to content

Commit

Permalink
fix(compiler-sfc): only keep the type when Boolean exists in prod mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ygj6 committed Oct 14, 2021
1 parent b77b219 commit 983409e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
Expand Up @@ -80,10 +80,11 @@ exports[`sfc props transform default values w/ type declaration, prod mode 1`] =
export default /*#__PURE__*/_defineComponent({
props: {
foo: { type: Number, default: 1 },
bar: { type: Object, default: () => {} },
baz: { type: null },
bool: { type: Boolean }
foo: { default: 1 },
bar: { default: () => {} },
baz: null,
boola: { type: Boolean },
boolb: { type: [Boolean, Number] }
},
setup(__props: any) {
Expand Down
Expand Up @@ -83,18 +83,19 @@ describe('sfc props transform', () => {
const { content } = compile(
`
<script setup lang="ts">
const { foo = 1, bar = {} } = defineProps<{ foo?: number, bar?: object, baz?: any, bool?: boolean }>()
const { foo = 1, bar = {} } = defineProps<{ foo?: number, bar?: object, baz?: any, boola?: boolean, boolb?: boolean | number }>()
</script>
`,
{ isProd: true }
)
// literals can be used as-is, non-literals are always returned from a
// function
expect(content).toMatch(`props: {
foo: { type: Number, default: 1 },
bar: { type: Object, default: () => {} },
baz: { type: null },
bool: { type: Boolean }
foo: { default: 1 },
bar: { default: () => {} },
baz: null,
boola: { type: Boolean },
boolb: { type: [Boolean, Number] }
}`)
assertCode(content)
})
Expand Down
10 changes: 6 additions & 4 deletions packages/compiler-sfc/src/compileScript.ts
Expand Up @@ -685,21 +685,23 @@ export function compileScript(
}
}
const { type, required } = props[key]
if (!isProd) {
const { type, required } = props[key]
return `${key}: { type: ${toRuntimeTypeString(
type
)}, required: ${required}${
defaultString ? `, ${defaultString}` : ``
} }`
} else {
const { type } = props[key]
// production: checks are useless
} else if (type.indexOf('Boolean') > -1) {
// production: if boolean exists, should keep the type.
return `${key}: { type: ${toRuntimeTypeString(
type
)}${
defaultString ? `, ${defaultString}` : ``
} }`
} else {
// production: checks are useless
return `${key}: ${defaultString ? `{ ${defaultString} }` : 'null'}`
}
})
.join(',\n ')}\n }`
Expand Down

0 comments on commit 983409e

Please sign in to comment.