-
-
Notifications
You must be signed in to change notification settings - Fork 8.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
v-bind cannot be bound to a type wrapped by a Partial #9639
Comments
Try this: <script setup lang="ts">
import { defineProps } from 'vue';
interface Conf {
foo: string;
}
const props = defineProps(['conf']);
</script>
<template>
<div v-bind="{ foo: conf.foo }" />
</template> It looks like TypeScript is being a bit finicky when we try to directly bind a prop with a Partial type using the v-bind directive in Vue. Let me break it down for you. In TypeScript, when we use Partial, we're essentially saying that all properties of T are optional. Now, when we attempt to bind such a partially defined prop using v-bind, it gets a bit confused. Vue's v-bind expects all properties to be present because it dynamically binds them. To make TypeScript happy and ensure a smooth binding process, we have a couple of options. One is to destructure the prop and bind its properties individually. In your case, it would look like this: <template>
<div v-bind="{ foo: conf.foo }" />
</template> This way, we explicitly state which properties we want to bind, and TypeScript is happy because it knows each property is there. |
@goktugerol-dev Thank you for your explanation. But I'm right to write it this way. |
@RJQingHuan Both ways of writing the code achieve a similar outcome in terms of the actual behavior in the template. But, the difference is how TypeScript interprets and checks the types. In In The actual behavior in the template should be similar, but the level of type checking and the strictness of TypeScript's analysis can change between the two approaches. Choose the one that aligns with the level of type safety you want in your code. It's a good practice to be aware of how TypeScript infers types, |
@goktugerol-dev Actually not right. Vue's |
I debugged this and it seems to be caused by non strict null check, please update This PR #9652 seems to fix this issue even with EDIT: I strongly recommend using I'll close this issue since as explained, we can only recommend using typescript strictly. |
Vue version
3.3.8
Link to minimal reproduction
https://play.vuejs.org/#eNp9kjFPwzAQhf/KyUuLVNqhWxQqQdUBBqigo5eQXIKLc7ZsJ1Sq8t85O7QUCXWJcve+l7yX+CjurZ33HYpM5L50ygbwGDoLuqDmTorgpVhJqrBWhC82KEN+egQqWsxgskMfJjDcSDohW2esz9N1NU17RQFdXZQIa0M1HCUB1MZk4INT1Ega/lLJO2IlGzLYFi6oQufRzlEYzxdjVJ54CNhaXQTkCSCvVA/97buiitPHB0gBC5byxQUnZlwsiqqZ770hbp9eGB2tVRrdT1UpsjFK1AqtzddT2gXX4ey0Lz+w/Pxnv/eHuJNi69Cj61GKsxYK12AY5c3bMx74/iy2puo001fEV/RGdzHjiD10VHHsCy6lfWyt4a9Hzc5vDgHJn0rFoJEcEi8FH4H1leq/cZfzZfLxjxDDN/ubwb0=
Steps to reproduce
View error information
What is expected?
no error
What is actually happening?
Use Partial to define the type of props and cannot be passed to the v-bind directive.
System Info
Any additional comments?
No response
The text was updated successfully, but these errors were encountered: