我想做一个reducer来修改一个appollo搜索变量。
type SearchReducer = Reducer<
WhereInput,
{ key: keyof WhereInput; value: WhereInput[keyof WhereInput] }
>;
useReducer<SearchReducer>((prev,{key,value})=>{
const newValue = {...prev};
// compile failure here
newValue[key] = value;
return newValue;
})
它说我不能假设 newValue[key] 与 value 共享相同的类型,我认为这是正确的,但是我如何限制我的类型中的两个 keyof 必须是相同的值?
答案 0 :(得分:0)
要限制键和值,您的 SearchReducer 类型可以重写如下:
type SearchReducer<T extends keyof WhereInput> = Reducer<
CaseWhereInput,
{ key: T; value: WhereInput[T] }
>;
虽然我不熟悉您使用的任何类型或您提到的 appollo 搜索(CaseWhereInput、WhereInput、Reducer)或签名 useReducer 函数,但很难为您提供完整的解决方案。