Work with TSX on vue 3 #6021
Replies: 3 comments
-
|
In TSX, Vue's
<MyComponent
user={myUser.value}
onUpdate:user={(val: UserType) => (myUser.value = val)}
/>
Full working example: import { defineComponent, ref } from 'vue';
const UserForm = defineComponent({
props: {
user: { type: Object as () => User, required: true },
},
emits: ['update:user'],
setup(props, { emit }) {
return () => (
<input
value={props.user.name}
onInput={(e) =>
emit('update:user', { ...props.user, name: (e.target as HTMLInputElement).value })
}
/>
);
},
});
const Parent = defineComponent({
setup() {
const user = ref<User>({ name: 'Alice' });
return () => (
<UserForm
user={user.value}
onUpdate:user={(val) => (user.value = val)}
/>
);
},
});TypeScript error on If the error persists after upgrading Volar, use a spread workaround for the colon-containing event name: {...{ 'onUpdate:user': (val: User) => (user.value = val) }} |
Beta Was this translation helpful? Give feedback.
-
|
In TSX, I would not rely on Babel can transform this: <UserForm
mode={PageMode.Create}
v-model:user={this.user}
permissions={this.permissions}
/>but TypeScript still sees a prop literally named user={this.user}
onUpdate:user={...}That is why TS complains that the required The TSX-friendly version is to write the expanded form yourself: <UserForm
mode={PageMode.Create}
user={this.user}
permissions={this.permissions}
onUpdate:user={(value: User) => {
this.user = value;
}}
/>Depending on your tooling, if <UserForm
mode={PageMode.Create}
user={this.user}
permissions={this.permissions}
{...{
"onUpdate:user": (value: User) => {
this.user = value;
},
}}
/>Your child component setup is basically correct: props: {
user: {
type: Object as PropType<User>,
required: true,
},
},
emits: ["update:user"],and inside this.$emit("update:user", nextUser);So the issue is not Vue runtime support. Vue 3 supports I would use the explicit prop plus update handler in TSX. It is also clearer and avoids making |
Beta Was this translation helpful? Give feedback.
-
|
The issue isn't Vue — it's TypeScript. TSX doesn't understand Vue's The fix — expand <UserForm
mode={PageMode.Create}
user={this.user}
permissions={this.permissions}
{...{ "onUpdate:user": (val: User) => { this.user = val; } }}
/>This is what
The spread syntax Your TL;DR: In Vue TSX, always expand |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi all
Can anyone tell me how to make VUE on TSX understand v-model with arguments?
I didn't find anything in the official documentation.
Using the latest version of vue 3 and ts 4.4.4 (if you need any more information write)
The problem is with TS. It doesn't understand the v-model construct, and since the user property is required, TS shows an error.
If make the user property optional, then this is normal for TS. And babel is already transpiling everything normally at the output.
Use Visual Studio Code with extensions Vue Language Features (Volar) v0.34.16 and TypeScript Vue Plugin (Volar) v0.34.16
Beta Was this translation helpful? Give feedback.
All reactions