Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types(runtime-core): improve the extracted instance types #1936

Merged
merged 4 commits into from
Aug 25, 2020
Merged

types(runtime-core): improve the extracted instance types #1936

merged 4 commits into from
Aug 25, 2020

Conversation

HcySunYang
Copy link
Member

@HcySunYang HcySunYang commented Aug 23, 2020

When using InstanceType to extract the component instance type and read props on it, the types of its props with default fields can still be undefined:

const Comp = defineComponent({
    props: {
        foo: {
            type: String,
            default: ''
        }
    }
})

const vm: = {} as InstanceType<typeof Comp>

vm.foo // string | undefined

This PR improves that.

@pikax
Copy link
Member

pikax commented Aug 23, 2020

You don't need to change the types, you can rebuild the instance types without changing the code:

const Comp = defineComponent({
  props: {
    foo: {
      type: String,
      default: ''
    }
  }
})

type BuildInstance<T> = T extends ComponentOptionsBase<
  infer Props,
  infer RawBindings,
  infer D,
  infer C,
  infer M,
  infer Mixin,
  infer Extends,
  infer E
>
  ? ComponentPublicInstance<Props, RawBindings, D, C, M, Mixin, Extends, E>
  : T

cc.foo // string

The constructor makes the props optional for the tsx.

I have this PR which should make a bit easier to maintain the DefineComponent types and also provide a better type extraction.

In this case I would recommend having a core type used to extract those types for the user land.

@HcySunYang
Copy link
Member Author

Is it this one: #1883? I just noticed it, I will close this PR.

@HcySunYang HcySunYang closed this Aug 23, 2020
@pikax
Copy link
Member

pikax commented Aug 23, 2020

Yep.

Forgot to paste the link 🤦‍♂️

Copy link
Member

@pikax pikax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes on the Public instance types are not necessary, you can just add them on defineComponent:

ComponentPublicInstanceConstructor<
  CreateComponentPublicInstance<
    ExtractPropTypes<PropsOptions, false>,
    RawBindings,
    D,
    C,
    M,
    Mixin,
    Extends,
    E,
    VNodeProps & AllowedComponentProps & ComponentCustomProps
  > 
& Readonly<ExtractPropTypes<PropsOptions, true>> // add this line to the `defineComponent` overloads
>

@HcySunYang HcySunYang reopened this Aug 23, 2020
@yyx990803 yyx990803 merged commit 955450f into vuejs:master Aug 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants