-
-
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
watch
callback arg has incorrect type for reactive array containing refs
#9416
Comments
By design, rawValue should be obtained, but it is a breaking change. |
Yes, I had a PR do this |
The type of You can perform an experiment by using <script lang="ts" setup>
import { watch, ref, reactive } from "vue";
const numbers = reactive([ref(0), ref(1), ref(2)]);
watch(
numbers,
(value) => {
// The type hint may indicate that there is no .value field, but the console output is still correct.
value.forEach((e) => console.log(e.value));
},
{
immediate: true,
}
);
</script> |
@Dunqing, I apologize, I've found that, as @Okysu mentioned, this is a When we use |
This seems to be working as expected: describe('should unwrap tuple correctly', () => {
const readonlyTuple = [ref(0)] as const
const reactiveReadonlyTuple = reactive(readonlyTuple)
expectType<Ref<number>>(reactiveReadonlyTuple[0])
const tuple: [Ref<number>] = [ref(0)]
const reactiveTuple = reactive(tuple)
expectType<Ref<number>>(reactiveTuple[0])
}) https://github.com/vuejs/core/blob/main/packages/dts-test/reactivity.test-d.ts#L55-L64 const numbers = reactive([ref(0), ref(1), ref(2)]); // [ ref(0), ...] When is used in conjunction with https://github.com/vuejs/core/blob/main/packages/runtime-core/src/apiWatch.ts#L211-L214 |
I have a try with your example, If you write a <script setup lang="ts">
import { watch, ref, reactive } from 'vue'
const numbers = reactive([ref(0), ref(1), ref(2)])
watch(() => numbers, (value) => {
// The type of the value is number[], but the value is [ref(0), ref(1), ref(2)]
console.log(value)
}, {
immediate: true,
})
</script>
<template>
<div>
{{ numbers }}
</div>
</template>
|
I believe the runtime behaviour is correct, but there does seem to be a problem with the types. To recap:
However, it seems that the types for import { ref, reactive, watch } from 'vue'
const num = ref(0)
const arr = reactive([num])
watch(arr, val => {
// val is an array containing the ref, but the types don't reflect that
console.log(val[0].value) // incorrect types
}, { immediate: true })
watch([num], val => {
// val is an array containing the value of ref, not the ref itself
console.log(val[0]) // correct types
}, { immediate: true }) Both of these playgrounds give TS errors for |
watch
is incorrectwatch
callback arg has incorrect type for reactive array containing refs
Vue version
latest
Link to minimal reproduction
https://play.vuejs.org/#eNp9UstOwzAQ/JWVL6RS1PK4VQUJEAc4AAJumEOabluXxLbsdVoU5d9ZO6GABL34sTPjGa/diktrx01AMRUzXzplCTxSsBdSq9oaR9DCtqBynYPDZRyKklSD0MHSmRqOWHsktdSl0Z5Ah3qOzsP5npi9si47HiV9djLMp6O3kdTp4GzQ5JA1RRVwBOcX0EoNMJnAyxqBPiyCWQLxOjFA+cHo9S2HeaDf0H+G8ciY0lQ4rsxqcJO6y3s7Vde4UAXhFMgFzBlhdDbp28IN4Q1hbSum8G62UA1PbTvk77pI7ouzyZ4ockGebZdqNd54o7nRyU2K0tRWVegeLCmOJcW0zxGxoqrM9i7Vhix9vVxj+f5HfeN3sSbFo0OPrkEp9hgVboXUwzfP97jj9R6szSJUzD4APiG3LMSMPe0q6AXH/sFLaW/Td1F69eJvdoTaf10qBo3MLvGl4B9zfeDq33HPxmdJxw8huk88WeOl
Steps to reproduce
What is expected?
I am not sure if the type is wrong or the value of the callback of the
watch
is wrongWhat is actually happening?
I am not sure, you can see the above
System Info
Any additional comments?
I would like to send a PR to fix this problem, but I need to know which solution is right first
The text was updated successfully, but these errors were encountered: