小编xua*_*uan的帖子

vue UnwrapRefSimple<T> 泛型类型无法在反应时分配给 T

我创建了一个钩子函数并想在其他地方重用它:

\n
import { ref, Ref, reactive } from 'vue'\n\nconst useSelectAarray = <T>(items: T[]) => {\n  const selectItem: Ref<T|null> = ref(null)\n  const options = reactive([...items])\n  const select = (key: string, value: any) => {\n    const trigger = options.find((item: { [index: string]: any }) => item[key] === value)\n\n    if (typeof trigger !== 'undefined') {\n      selectItem.value = trigger\n    }\n  }\n\n  return {  \n    selectItem,\n    select,\n    items: options\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

但是在selectItem.value = trigger,我收到类型错误Type 'UnwrapRefSimple<T>' is not assignable to type 'T'\xe3\x80\x82 …

typescript typescript-generics vuejs3

15
推荐指数
1
解决办法
1万
查看次数

标签 统计

typescript ×1

typescript-generics ×1

vuejs3 ×1