-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
Using props in tsx is not possible #2417
Comments
I got the same here. Just created a component
And tried to use it in another one
Than I got the error
If instead of the |
@Leandro-Albano your solution does not provide valid type checking for props. Bump the topic |
same problem for me |
@Akryum OP has already provided a reproduction link. you can reproduce by |
Same problem for me...is there any workaround that would keep the type-checking? |
Same here |
@Akryum @yyx990803 Can we please get any update on that? |
I have just cloned my repo, ran a fresh
|
I've managed to resolve the problem for my scenario using the following pattern First of all I've extended the JSX.ElementAttributesProperty interface and created an abstract Vue extension class to serve as a base for the component. So let's create a ts file [e.g. vue-tsx.ts] somewhere in your project and place the following in there
Now the component has to be created using following pattern - It's essential that there's an interface defined and implemented by the component - ensures the type-safety. This is passed to the TsxComponent base class as a generic argument which creates the dummy vueTsxProps property (see the abstract class above), which is served to the TS typechecker as the valid parameter list. So let's create a sample component like this
Now when using the component, correct type-checking is applied [1st one denied due to missing mandatory property, 2nd one ok, 3rd one denied due to unknown property applied] |
@brunolau It's a nice hack, but it forces you to write types and prop declarations twice, which doesn't seem like a good idea :/ |
Yes, that's the downside of the approach. However you are still protected by the compiler type checking. As you are both passing the interface type to the base class as well as implementing it on the component class, you are getting protected from making a typo on either side Let's figure you want to add property "birthYear: number" to the component. If you forget to add it to the interface, you will get compile error from the TSX template and if you forget to add it to the component class, you will get compile error stating that the component class doesn't implement the interface. Still, I fully agree that having to write the property twice isn't the ideal approach, however I wasn't able to find any other way besides not using the property decorators. [If you want to stick with props: {} annotation rather than property decorators, just change the "vueTsxProps" to "props" from my example and you should be good. There are also some other ways for property decorators, these will however make all the component properties optional as well as include all Vue base class properties in the intellisense, for more info see This issue on Typescript repo As I wanted to stick with property decorators, I've decided to gowith the approach described above and I'm affraid that until the Vue version 3 is out, one will always have to choose the "smaller evil" |
That was not a solution, I'm just giving as much info as I have about the issue. Maybe I should say that it compiles fine, not that it works fine. |
I have been using this library to great success, you can get typechecking for both props and events. There is also a
|
I'm not sure that we should solve this within vue-cli. The state of TSX and Vue 2.* simply is what we see here: without using a lib like Seeing as that lib also has a cli-plugin already, I'm not sure we can do much in CLI core to further improve the sitution. CommentS? |
Thank you for this answer. I was not aware of |
I agree with @LinusBorg ... until Vue 3 hits, TSX can be supported by a third party lib. Although I think when Vue 3 does come out, TS support should be a first class citizen (including TSX) in Vue CLI. @DCzajkowski |
I'll close this then. Nothing further to do. |
It's |
For me the work around was by updating the
I know its after a very long time. Just incase someone also fetches the same issue. |
It makes me crazy to set up a project with typescript and jsx. From now, I should use React if wanna work with jsx. Too many problems |
I donot know why, but it works.😂 |
Version
3.0.1
Reproduction link
https://github.com/DCzajkowski/vue-jsx-reproduction
Node and OS info
Node v9.11.2 / yarn 1.9.4 / macOS
Steps to reproduce
What is expected?
Compilation successful.
What is actually happening?
An error appears
I am very sorry if this is a set-up problem on my side or if it's not a correct repo to report this kind of stuff. I don't have experience with TSX and really can't find a solution to why this may not work. Sorry if it's a duplicate.
P.S. is vuejs/vue#6856 related?
The text was updated successfully, but these errors were encountered: