Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(compiler-sfc): add type for props's properties in prod mode (#4790)
fix #4783
  • Loading branch information
ygj6 committed Nov 3, 2021
1 parent d56f115 commit 090df08
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
Expand Up @@ -82,7 +82,9 @@ export default /*#__PURE__*/_defineComponent({
props: {
foo: { default: 1 },
bar: { default: () => {} },
baz: null
baz: null,
boola: { type: Boolean },
boolb: { type: [Boolean, Number] }
},
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 }>()
const { foo = 1, bar = {} } = defineProps<{ foo?: number, bar?: object, baz?: any, boola?: boolean, boolb?: boolean | number }>()
</script>
`,
{ isProd: true }
Expand All @@ -93,7 +93,9 @@ describe('sfc props transform', () => {
expect(content).toMatch(`props: {
foo: { default: 1 },
bar: { default: () => {} },
baz: null
baz: null,
boola: { type: Boolean },
boolb: { type: [Boolean, Number] }
}`)
assertCode(content)
})
Expand Down
25 changes: 15 additions & 10 deletions packages/compiler-sfc/src/compileScript.ts
Expand Up @@ -685,13 +685,20 @@ 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 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'}`
Expand Down Expand Up @@ -1621,15 +1628,13 @@ function extractRuntimeProps(
m.key.type === 'Identifier'
) {
let type
if (!isProd) {
if (m.type === 'TSMethodSignature') {
type = ['Function']
} else if (m.typeAnnotation) {
type = inferRuntimeType(
m.typeAnnotation.typeAnnotation,
declaredTypes
)
}
if (m.type === 'TSMethodSignature') {
type = ['Function']
} else if (m.typeAnnotation) {
type = inferRuntimeType(
m.typeAnnotation.typeAnnotation,
declaredTypes
)
}
props[m.key.name] = {
key: m.key.name,
Expand Down

0 comments on commit 090df08

Please sign in to comment.