Skip to content

[vue3] Props type inference breaks, if custom-validator is present #2738

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

Closed
MarcRoemmelt opened this issue Dec 5, 2020 · 4 comments
Closed

Comments

@MarcRoemmelt
Copy link

MarcRoemmelt commented Dec 5, 2020

Version

3.0.4

Reproduction link

https://github.com./MarcRoemmelt/vue-lost-proptypes

Steps to reproduce

  1. Clone project
  2. Install dependencies
  3. Check App.vue intellisense for props-typings in setup(props) or this.$props

What is expected?

Type inference for all props should work regardless of custom-validator

What is actually happening?

If a validator function is present in any prop, the type inference breaks. Interestingly, if the validator is an arrow-function it it works.

image

@yyx990803
Copy link
Member

This is a known issue and is a limitation of TypeScript handling circular inference of ThisType (which is only present if using a non-arrow function).

There is currently no fix for this (will have to be upstream in TS) so using arrow function is the recommended workaround.

@kuanyui
Copy link

kuanyui commented Mar 10, 2021

This issue still exists even if using an arrow function as the validator of a prop. (vue 3.0.7, typescript 4.2.3)

Screenshot_20210310_162646
Screenshot_20210310_162936

@JensDll
Copy link
Contributor

JensDll commented Mar 10, 2021

@kuanyui You will still need to type the validators parameter:

size: {
  type: String,
  default: 'md',
  validator: (x: string) => { ... }
}

@kuanyui
Copy link

kuanyui commented Mar 10, 2021

@kuanyui You will still need to type the validators parameter:

size: {
  type: String,
  default: 'md',
  validator: (x: string) => { ... }
}

@JensDll Wow magic it works! So fantastic! Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants