Skip to content

Commit 090df08

Browse files
authored
fix(compiler-sfc): add type for props's properties in prod mode (#4790)
fix #4783
1 parent d56f115 commit 090df08

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

packages/compiler-sfc/__tests__/__snapshots__/compileScriptPropsTransform.spec.ts.snap

+3-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ export default /*#__PURE__*/_defineComponent({
8282
props: {
8383
foo: { default: 1 },
8484
bar: { default: () => {} },
85-
baz: null
85+
baz: null,
86+
boola: { type: Boolean },
87+
boolb: { type: [Boolean, Number] }
8688
},
8789
setup(__props: any) {
8890

packages/compiler-sfc/__tests__/compileScriptPropsTransform.spec.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ describe('sfc props transform', () => {
8383
const { content } = compile(
8484
`
8585
<script setup lang="ts">
86-
const { foo = 1, bar = {} } = defineProps<{ foo?: number, bar?: object, baz?: any }>()
86+
const { foo = 1, bar = {} } = defineProps<{ foo?: number, bar?: object, baz?: any, boola?: boolean, boolb?: boolean | number }>()
8787
</script>
8888
`,
8989
{ isProd: true }
@@ -93,7 +93,9 @@ describe('sfc props transform', () => {
9393
expect(content).toMatch(`props: {
9494
foo: { default: 1 },
9595
bar: { default: () => {} },
96-
baz: null
96+
baz: null,
97+
boola: { type: Boolean },
98+
boolb: { type: [Boolean, Number] }
9799
}`)
98100
assertCode(content)
99101
})

packages/compiler-sfc/src/compileScript.ts

+15-10
Original file line numberDiff line numberDiff line change
@@ -685,13 +685,20 @@ export function compileScript(
685685
}
686686
}
687687
688+
const { type, required } = props[key]
688689
if (!isProd) {
689-
const { type, required } = props[key]
690690
return `${key}: { type: ${toRuntimeTypeString(
691691
type
692692
)}, required: ${required}${
693693
defaultString ? `, ${defaultString}` : ``
694694
} }`
695+
} else if (type.indexOf('Boolean') > -1) {
696+
// production: if boolean exists, should keep the type.
697+
return `${key}: { type: ${toRuntimeTypeString(
698+
type
699+
)}${
700+
defaultString ? `, ${defaultString}` : ``
701+
} }`
695702
} else {
696703
// production: checks are useless
697704
return `${key}: ${defaultString ? `{ ${defaultString} }` : 'null'}`
@@ -1621,15 +1628,13 @@ function extractRuntimeProps(
16211628
m.key.type === 'Identifier'
16221629
) {
16231630
let type
1624-
if (!isProd) {
1625-
if (m.type === 'TSMethodSignature') {
1626-
type = ['Function']
1627-
} else if (m.typeAnnotation) {
1628-
type = inferRuntimeType(
1629-
m.typeAnnotation.typeAnnotation,
1630-
declaredTypes
1631-
)
1632-
}
1631+
if (m.type === 'TSMethodSignature') {
1632+
type = ['Function']
1633+
} else if (m.typeAnnotation) {
1634+
type = inferRuntimeType(
1635+
m.typeAnnotation.typeAnnotation,
1636+
declaredTypes
1637+
)
16331638
}
16341639
props[m.key.name] = {
16351640
key: m.key.name,

0 commit comments

Comments
 (0)