-
-
Notifications
You must be signed in to change notification settings - Fork 8.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
Typescript: incorrect typings for unwrapped refs #3315
Comments
It's not a bug.this is intentional in options api. you will see your expected use setup(){
const arr = [1, 2, 3].map((num) => ({ num: ref(num) }))
arr.forEach((el) => {
console.log(el.num);
console.log(el.num.value);
});
} |
If it is true than I have a lot of questions:
If 1-4 is not true, and it's actually works than why typescript should tell me it doesn't? PS. More appropriate example of composition API would be import { defineComponent, ref, reactive } from "vue";
export default defineComponent({
setup() {
const r = reactive({
arr: [1, 2, 3].map((num) => ({ num: ref(num) }))
});
r.arr.forEach(el => { console.log(el.num) });
}
}); It all about unwrapping. PS2. Example is a bit overcomplicated (array of objects instead of simple ref) because I couldn't reproduce issue with just ref. I can now, and it's confusing. Don't know if either different IDE or different packages or whatever made the difference. |
It should be data: () => ({
arr: [1, 2, 3].map((num) => ({ num })),
}), without Please, next time consider using the forum, the Discord server or StackOverflow for questions first. But feel free to come back and open an issue if it turns out to be a bug 🙂 |
@posva, the questions is not about how to use it right, as I need ref in real case. Some usecases is in previous post. I didn't see in docs that refs can't be used in options API. Runtime does not warn if I do it. It even seems to work. Except correct typescript support. If using refs in data is forbidden, that I would expect core maintainers to explicitly say this, not some people on forum. Because it has significant consequences (look previous post) and makes options API discouraged legacy way of writing components |
@posva He has a point. I don't think we have a rule of ref being forbidden in |
In general, the Options API and the Composition API shouldn't be mixed. When using composition functions that return Refs, one should use However, you are both right about the types: it should properly infer it |
Version
3.0.6
Reproduction link
https://github.com/shameleo/vue-ts-bug
Steps to reproduce
Open
index.html
browser and look console output.What is expected?
If typescript is correct,
console.log
would print refs and their values.What is actually happening?
I see values and
undefined
Here essential code:
The text was updated successfully, but these errors were encountered: