fix(compiler-sfc): generate matching prop types when withDefaults is used (#4466)
fix #4455
This commit is contained in:
@@ -1037,11 +1037,13 @@ import { defaults } from './foo'
|
||||
export default /*#__PURE__*/_defineComponent({
|
||||
props: _mergeDefaults({
|
||||
foo: { type: String, required: false },
|
||||
bar: { type: Number, required: false }
|
||||
bar: { type: Number, required: false },
|
||||
baz: { type: Boolean, required: true }
|
||||
}, { ...defaults }) as unknown as undefined,
|
||||
setup(__props: {
|
||||
foo?: string
|
||||
bar?: number
|
||||
baz: boolean
|
||||
}, { expose }) {
|
||||
expose()
|
||||
|
||||
@@ -1060,12 +1062,11 @@ exports[`SFC compile <script setup> with TypeScript withDefaults (static) 1`] =
|
||||
export default /*#__PURE__*/_defineComponent({
|
||||
props: {
|
||||
foo: { type: String, required: false, default: 'hi' },
|
||||
bar: { type: Number, required: false }
|
||||
bar: { type: Number, required: false },
|
||||
baz: { type: Boolean, required: true },
|
||||
qux: { type: Function, required: false, default() { return 1 } }
|
||||
} as unknown as undefined,
|
||||
setup(__props: {
|
||||
foo?: string
|
||||
bar?: number
|
||||
}, { expose }) {
|
||||
setup(__props: { foo: string, bar?: number, baz: boolean, qux(): number }, { expose }) {
|
||||
expose()
|
||||
|
||||
const props = __props
|
||||
|
||||
@@ -800,8 +800,11 @@ const emit = defineEmits(['a', 'b'])
|
||||
const props = withDefaults(defineProps<{
|
||||
foo?: string
|
||||
bar?: number
|
||||
baz: boolean
|
||||
qux?(): number
|
||||
}>(), {
|
||||
foo: 'hi'
|
||||
foo: 'hi',
|
||||
qux() { return 1 }
|
||||
})
|
||||
</script>
|
||||
`)
|
||||
@@ -810,10 +813,19 @@ const emit = defineEmits(['a', 'b'])
|
||||
`foo: { type: String, required: false, default: 'hi' }`
|
||||
)
|
||||
expect(content).toMatch(`bar: { type: Number, required: false }`)
|
||||
expect(content).toMatch(`baz: { type: Boolean, required: true }`)
|
||||
expect(content).toMatch(
|
||||
`qux: { type: Function, required: false, default() { return 1 } }`
|
||||
)
|
||||
expect(content).toMatch(
|
||||
`{ foo: string, bar?: number, baz: boolean, qux(): number }`
|
||||
)
|
||||
expect(content).toMatch(`const props = __props`)
|
||||
expect(bindings).toStrictEqual({
|
||||
foo: BindingTypes.PROPS,
|
||||
bar: BindingTypes.PROPS,
|
||||
baz: BindingTypes.PROPS,
|
||||
qux: BindingTypes.PROPS,
|
||||
props: BindingTypes.SETUP_CONST
|
||||
})
|
||||
})
|
||||
@@ -825,6 +837,7 @@ const emit = defineEmits(['a', 'b'])
|
||||
const props = withDefaults(defineProps<{
|
||||
foo?: string
|
||||
bar?: number
|
||||
baz: boolean
|
||||
}>(), { ...defaults })
|
||||
</script>
|
||||
`)
|
||||
@@ -834,7 +847,8 @@ const emit = defineEmits(['a', 'b'])
|
||||
`
|
||||
_mergeDefaults({
|
||||
foo: { type: String, required: false },
|
||||
bar: { type: Number, required: false }
|
||||
bar: { type: Number, required: false },
|
||||
baz: { type: Boolean, required: true }
|
||||
}, { ...defaults })`.trim()
|
||||
)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user