-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Using generic for ref()
causes error when reassigning
#6766
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
Comments
Unfortunately this is because ref values have to expose the "unwrapped" type, which unwraps nested refs, to ensure value access correctness: const a = { b: ref(0) }
const c = ref(a)
c.value.b // should be number
c.value = a // same error you saw To fix this we will need TypeScript to allow specifying different type constraints for getter and setter of the same property. See microsoft/TypeScript#43662 |
@yyx990803 understood. Perhaps at least we can deal with generics that extend primitive types? https://stackblitz.com/edit/vitejs-vite-hmunsf?file=src%2Fmain.ts E.g. const f = <T extends string>(someT: T) => {
const t = ref(someT);
t.value = someT; // expected no error
};
const g = <T extends number>(someT: T) => {
const t = ref(someT);
t.value = someT; // expected no error
}; In these cases, we can be sure that |
Now that that PR was merged into TS 5.1, can this get some attention? I think this is a pretty common pattern, which I totally expected to work when Vue 3.3 landed. |
What should we do about generics? function test<T extends Record<string, any>>(someT: T) {
const t = ref(someT)
t.value = someT // errors
}; |
Vue version
3.2.40
Link to minimal reproduction
https://stackblitz.com/edit/vitejs-vite-rpmhsh?file=src/main.ts
Steps to reproduce
ref(t)
wheret
is of type genericT
will cause error when reassigning the ref to a value ofT
.What is expected?
expected
refOfT.value = t
to workWhat is actually happening?
Got an error
System Info
No response
Any additional comments?
No response
The text was updated successfully, but these errors were encountered: