core
core copied to clipboard
refactor(types): refactor the `InferPropType` type
Fix: #4689 Please allow me to remind you pay attention to this pr. Due to historic reasons, Props's type inference has two styles.
vue2 style
const props = {
msg1: Number,
msg2: {
type: Number,
required: true
}
}
defineComponent<typeof props>({
setup(props) {
//....
}
})
TS style
const MyComponent = defineComponent<{ a?: number; msg: number }>({
setup(props) {
//.....
}
})
msg2 is required, msg1 not.
It's easily see what the differences:
- If we defined one prop is vue2 style, we must use a special field called
requiredto ensure it's required. - If we defined one prop is TS style, we can just use
?to control whether required.
It seems that they are easily distinguished, but it's not a easy thing in the code.
Like #4689, there is a minimum repetition:

Because it mixed vue2 style and TS style, but we did not consider this before.
A digression
I checked some history commite about here, it seems had been modified many times. I believe that had affected the maintain.
eg

No offence, it may seem strange, why Date, DateConstructor had be special treatment, so I choose to refactor InferPropType.
✔️ Deploy Preview for vuejs-coverage ready!
🔨 Explore the source changes: 35f59b3c2274711cdc0372a4e6258a37b4cd875e
🔍 Inspect the deploy log: https://app.netlify.com/sites/vuejs-coverage/deploys/61f2b6e207303d000774e57f
😎 Browse the preview: https://deploy-preview-5031--vuejs-coverage.netlify.app
✔️ Deploy Preview for vue-sfc-playground ready!
🔨 Explore the source changes: 35f59b3c2274711cdc0372a4e6258a37b4cd875e
🔍 Inspect the deploy log: https://app.netlify.com/sites/vue-sfc-playground/deploys/61f2b6e257459f00071bd3fc
😎 Browse the preview: https://deploy-preview-5031--vue-sfc-playground.netlify.app/
✔️ Deploy Preview for vue-next-template-explorer ready!
🔨 Explore the source changes: 35f59b3c2274711cdc0372a4e6258a37b4cd875e
🔍 Inspect the deploy log: https://app.netlify.com/sites/vue-next-template-explorer/deploys/61f2b6e220abbe0009195dd9
😎 Browse the preview: https://deploy-preview-5031--vue-next-template-explorer.netlify.app