Skip to content

BUG | useIntersectionObserver | useTemplateRef of array type cannot be passed as argument #4712

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

Open
7 tasks done
zaalbarxx opened this issue Apr 16, 2025 · 2 comments
Open
7 tasks done
Labels
bug Something isn't working

Comments

@zaalbarxx
Copy link

Describe the bug

Given

<template>
<div v-for="event in events" ref="$events"></div>
</template>

<script setup lang="ts">
...
const $events = useTemplateRef('$events');
useIntersectionObserver($events, () => {});
</script>

Image

where $events is a ref with array of elements and ref="$events" bound to element which has v-for it will mark template ref's value as Readonly<ShallowRef<HTMLDivElement[] | null>> which is incompatible with MaybeComputedElementRef | MaybeRefOrGetter<MaybeElement[]> | MaybeComputedElementRef[].

Of course it can be mitigated by using computed(() => $events.value ?? []) and it will work fine, but it would propably be possible to improve the input types for the composable itself (did not have time yet to look closer into it).

For reproduction type vue-tsc -p tsconfig.app.json in terminal.

Reproduction

https://stackblitz.com/edit/vitejs-vite-cadjhx57?file=src%2Fviews%2FAbout.vue,src%2Fcomponents%2FCounter.vue,src%2Fcomponents%2FHeader.vue,src%2Fviews%2FHome.vue,src%2FApp.vue

System Info

Windows 11
Vue 3.5.13
VueUse 13.1.0

Used Package Manager

pnpm

Validations

@ilyaliao ilyaliao added bug Something isn't working and removed pending triage labels May 29, 2025
@ilyaliao
Copy link
Member

It looks like a type edge case that doesn't affect usage, but if you want to avoid type errors, here is a temporary solution.

@geminigeek
Copy link

It looks like a type edge case that doesn't affect usage, but if you want to avoid type errors, here is a temporary solution.

hi, i think this should be shown in docs, on how to use with v-for using "useTemplateRefsList" its interesting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants