vueuse / vueuse

Collection of essential Vue Composition Utilities for Vue 2 and 3

Home Page:https://vueuse.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`useTimeout`: Support `MaybeRefOrGetter` argument type

Heniker opened this issue · comments

Clear and concise description of the problem

useTimeout is the only function that uses number as it's interval argument. useTimeoutPoll, useInterval, useTimeoutFn all support specifying MaybeRefOrGetter type.

Suggested solution

Change number type to MaybeRefOrGetter<number>

Alternative

No response

Additional context

https://vueuse.org/shared/useTimeout
https://github.com/vueuse/vueuse/blob/main/packages/shared/useTimeout/index.ts

Validations

In my opinion, if we pass a variable of type MaybeRefOrGetter as an argument to a composable, one can expect that if the value changes, the composable will react to those changes, but in fact, it won't - the interval won't be changed. That's why I think it is better to have a number type for that.

The following code will work correctly ATM, aside from TS error:

const t = ref(1000);
const { ready, start, stop } = useTimeout(t, { controls: true });
console.log(ready.value); // false
await promiseTimeout(1100);
console.log(ready.value); // true

t.value = 2000;
start();
await promiseTimeout(1100);
console.log(ready.value); // false
await promiseTimeout(1100);
console.log(ready.value); // true

This is expected and reasonable behavior.

My point is not about reactivity - it's about inconsistency with other functions. I can't see why it requires any justification.