-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
useVModels mistakenly marks returned ref as possibly undefined if the property is optional #3546
Comments
Hi! I think this type error has nothing to do with vueuse. const props = withDefaults(defineProps<{
foo: string
bar?: string
}>(), {
bar: undefined,
}) |
I believe it has to do with vueuse, because it's I realise I can work around the problem this way or another; my point is that these workarounds should be included with |
I found that this error also exists in const { foo, bar } = toRefs(props) So this is not necessarily caused by |
When using const props: { foo: string; bar: string | undefined } = {
foo: '',
bar: undefined,
}
const { foo, bar } = useVModels(props, emit)
foo.value = 'foo' // OK
bar.value = 'bar' // OK vueuse/packages/core/useVModels/index.ts Lines 23 to 39 in f3f5f02
|
So this kind of behaviour is fine and expectable for
Vue already and knowingly does that in const props = defineProps<{
foo: string;
bar?: string;
}>(); will always have
Since |
We need to clarify that its definition is a TypeScript specification, which means that the Of course, we can also return a Proxy for destructuring, which is sufficient to ensure that the returned value is a |
Basically, instead of returning Granted, that should be Vue concern in the first place, but if vueuse is a collection of hacks on top of Vue, it might do that on its own. I totally understand if you don't want to do that, though. The implementation could look like: type NotPossiblyUndefinedKeys<T> = {
[K in keyof T]: undefined extends T[K] ? never : K
}[keyof T]
type PossiblyUndefinedKeys<T> = {
[K in keyof T]: undefined extends T[K] ? K : never
}[keyof T]
type FixProps<T> = {
[K in NotPossiblyUndefinedKeys<T>]: T[K]
} & {
[K in PossiblyUndefinedKeys<T>]-?: T[K] | undefined
} I am not proud of it, it's better to simplify if possible, I just didn't manage to achieve that without splitting keys. |
This property needs to be iterable to ensure that it can be included in the new object. |
This is fixed in upstream: vuejs/core#6421 |
Describe the bug
useVModels
mistakenly marks the returned ref object as possibly undefined if the respective property is optional.This is not correct. The ref is always returned. The ref value is possibly undefined, but not the ref object itself.
Example:
How to reproduce: in StackBlitz, run
npm test
.Reproduction
https://stackblitz.com/edit/nuxt-starter-vj3utk?file=components%2Ftest.vue
System Info
System: OS: Linux 5.0 undefined CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz Memory: 0 Bytes / 0 Bytes Shell: 1.0 - /bin/jsh Binaries: Node: 18.18.0 - /usr/local/bin/node Yarn: 1.22.19 - /usr/local/bin/yarn npm: 9.4.2 - /usr/local/bin/npm pnpm: 8.9.2 - /usr/local/bin/pnpm npmPackages: @vueuse/core: ^10.6.1 => 10.6.1
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: