You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the current version of Vue, when using customRef to create custom refs, it’s required that the get and set functions have the same type. Otherwise, it results in a TypeScript error. However, I would like customRef to support separate types for the get and set functions to meet specific requirements.
Details
I am attempting to write the following code to fulfill a particular use case:
<scriptsetuplang="ts">import { customRef, typeRef } from'vue';asyncfunction fetchSuggestions(keyword=''):Promise<string[]> {awaitnewPromise(resolve=>setTimeout(resolve, Math.random() *500+500));return [`random suggestion for '${keyword}'`];}function switchAsyncRef<T>(initial:Promise<T>):Ref<T|null> {let id =0;let value:T|null=null;const myRef =customRef((track, trigger) => ({// 🚫 Type '() => T | null' is not assignable to type '() => T | Promise<T>'. get():typeofvalue {track();returnvalue; },async set(newValue:Promise<T> |T) {const currentId =++id;const resolvedValue =awaitnewValue;if (currentId!==id) return;value=resolvedValue;trigger(); }, }));myRef.value=initial;// 🚫 Type 'Ref<unknown>' is not assignable to type 'Ref<T | null>'.returnmyRef;}const suggestions =switchAsyncRef(fetchSuggestions());</script>
<template>
<inputtype="text"
@input=" (suggestions as unknown as Promise<typeof suggestions>) =fetchSuggestions(($event.target as HTMLInputElement).value)"
/>
<ulv-if="suggestions">
<liv-for="suggestion in suggestions">{{ suggestion }}</li>
</ul>
</template>
Expected Behavior
I expect customRef to support separate types for the get and set functions, enabling the successful compilation of the above code.
Currently, TypeScript reports errors because the get and set functions have incompatible types. Additionally, the use of suggestions in the template also requires type assertion, which is something I’d prefer to avoid.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Problem Overview
In the current version of Vue, when using
customRef
to create custom refs, it’s required that theget
andset
functions have the same type. Otherwise, it results in a TypeScript error. However, I would likecustomRef
to support separate types for theget
andset
functions to meet specific requirements.Details
I am attempting to write the following code to fulfill a particular use case:
Expected Behavior
I expect
customRef
to support separate types for theget
andset
functions, enabling the successful compilation of the above code.BTW, TypeScript can do this:
Actual Behavior
Currently, TypeScript reports errors because the
get
andset
functions have incompatible types. Additionally, the use ofsuggestions
in the template also requires type assertion, which is something I’d prefer to avoid.Additional Information
Vue: 3.3.4
TypeScript: 5.1.6
Beta Was this translation helpful? Give feedback.
All reactions