Skip to content

Commit 4e5d9cd

Browse files
authored
refactor(compiler-sfc): drop Function prop type when no static default value (#7125)
1 parent 0187f99 commit 4e5d9cd

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

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

+23
Original file line numberDiff line numberDiff line change
@@ -1784,6 +1784,29 @@ const props = __props as { foo: string, bar?: number, baz: boolean, qux(): numbe
17841784

17851785

17861786

1787+
return { props }
1788+
}
1789+
1790+
})"
1791+
`;
1792+
1793+
exports[`SFC compile <script setup> with TypeScript withDefaults (static) w/ production mode 1`] = `
1794+
"import { defineComponent as _defineComponent } from 'vue'
1795+
1796+
export default /*#__PURE__*/_defineComponent({
1797+
props: {
1798+
foo: null,
1799+
bar: { type: Boolean },
1800+
baz: { type: [Boolean, Function], default: true },
1801+
qux: { default: 'hi' }
1802+
},
1803+
setup(__props: any, { expose }) {
1804+
expose();
1805+
1806+
const props = __props as { foo: () => void, bar: boolean, baz: boolean | (() => void), qux: string | number };
1807+
1808+
1809+
17871810
return { props }
17881811
}
17891812

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

+30
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,36 @@ const emit = defineEmits(['a', 'b'])
11211121
})
11221122
})
11231123

1124+
// #7111
1125+
test('withDefaults (static) w/ production mode', () => {
1126+
const { content } = compile(
1127+
`
1128+
<script setup lang="ts">
1129+
const props = withDefaults(defineProps<{
1130+
foo: () => void
1131+
bar: boolean
1132+
baz: boolean | (() => void)
1133+
qux: string | number
1134+
}>(), {
1135+
baz: true,
1136+
qux: 'hi'
1137+
})
1138+
</script>
1139+
`,
1140+
{ isProd: true }
1141+
)
1142+
assertCode(content)
1143+
expect(content).toMatch(`const props = __props`)
1144+
1145+
// foo has no default value, the Function can be dropped
1146+
expect(content).toMatch(`foo: null`)
1147+
expect(content).toMatch(`bar: { type: Boolean }`)
1148+
expect(content).toMatch(
1149+
`baz: { type: [Boolean, Function], default: true }`
1150+
)
1151+
expect(content).toMatch(`qux: { default: 'hi' }`)
1152+
})
1153+
11241154
test('withDefaults (dynamic)', () => {
11251155
const { content } = compile(`
11261156
<script setup lang="ts">

packages/compiler-sfc/src/compileScript.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -822,8 +822,15 @@ export function compileScript(
822822
)}, required: ${required}${
823823
defaultString ? `, ${defaultString}` : ``
824824
} }`
825-
} else if (type.some(el => el === 'Boolean' || el === 'Function')) {
826-
// #4783, #7111 for boolean or function, should keep the type
825+
} else if (
826+
type.some(
827+
el =>
828+
el === 'Boolean' ||
829+
((!hasStaticDefaults || defaultString) && el === 'Function')
830+
)
831+
) {
832+
// #4783 for boolean, should keep the type
833+
// #7111 for function, if default value exists or it's not static, should keep it
827834
// in production
828835
return `${key}: { type: ${toRuntimeTypeString(type)}${
829836
defaultString ? `, ${defaultString}` : ``

0 commit comments

Comments
 (0)