core icon indicating copy to clipboard operation
core copied to clipboard

refactor(types): refactor the `InferPropType` type

Open caozhong1996 opened this issue 4 years ago • 3 comments

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 required to 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:

impicture_20211203_203831

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 impicture_20211207_113915

No offence, it may seem strange, why Date, DateConstructor had be special treatment, so I choose to refactor InferPropType.

caozhong1996 avatar Dec 03 '21 12:12 caozhong1996

✔️ 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

netlify[bot] avatar Jan 27 '22 15:01 netlify[bot]

✔️ 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/

netlify[bot] avatar Jan 27 '22 15:01 netlify[bot]

✔️ 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

netlify[bot] avatar Jan 27 '22 15:01 netlify[bot]