vue-cli: Using props in tsx is not possible
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
$ yarn && yarn serve
What is expected?
Compilation successful.
What is actually happening?
An error appears
ERROR in /Users/Darek/web/sites/test2-vue-project/src/views/Home.tsx
9:21 Property 'msg' does not exist on type 'ComponentOptions<Vue, DefaultData<Vue>, DefaultMethods<Vue>, DefaultComputed, PropsDefinition<Record<string, any>>, Record<string, any>> | ThisTypedComponentOptionsWithArrayProps<Vue, object, object, object, never> | ThisTypedComponentOptionsWithRecordProps<...> | undefined'.
7 | return (
8 | <div class='home'>
> 9 | <HelloWorld msg='Welcome to Your Vue.js TypeScript App' />
| ^
10 | </div>
11 | )
12 | }
No lint errors found
Version: typescript 3.0.3, tslint 5.11.0
Time: 9742ms
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 https://github.com/vuejs/vue/pull/6856 related?
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 22
- Comments: 22 (4 by maintainers)
I got the same here.
Just created a component
And tried to use it in another one
Than I got the error
Property 'widget' does not exist on type 'ThisTypedComponentOptionsWithArrayProps<Vue, object, object, object, never> | ThisTypedComponentOptionsWithRecordProps<Vue, object, object, object, object> | ComponentOptions<...> | undefined'.If instead of the
<NavLink widget='pushmenu'>I use<nav-link widget='pushmenu'>it works fine. Or, if the attribute wassomething-widgetinstead of justwidget, it works fine too.For me the work around was by updating the
shims-tsx.d.tsfileI know its after a very long time. Just incase someone also fetches the same issue.
I’ve managed to resolve the problem for my scenario using the following pattern [sorry if the guide is a bit longer and not suitable for github, but might help some people having the same problem]
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]
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
It’s
vue add tsx-support, just in case someone wonders, why they get an 404 😃@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 😕
@Akryum OP has already provided a reproduction link. If it’s out-of-date or so, I just made another one: vue-tsx
you can reproduce by
npm installnpm run serveand then inpsect the typescript type-check error (the code compiles and works as expected, but the tsc emits an error and so does the VSCode)I’ll close this then. Nothing further to do.
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
vue-tsx-support, TSX won’t work reliably. (sidenote: TSX should work out of the box with Vue 3.)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?
I have been using this library to great success, you can get typechecking for both props and events. https://github.com/wonderful-panda/vue-tsx-support
There is also a
vue-cli-pluginto configure it for you.