如何解决Typescript Object.entries 和映射类型
尝试使用 unknown
类型正确键入 Object.entries 返回值,如下所示:
export type BinaryRelation<T> = (x: T) => (y: T) => boolean;
export type SetoidFn<T> = BinaryRelation<T>;
export const setoidFnfrom = <A>(equals: BinaryRelation<A>): SetoidFn<A> =>
(x) => (y) => x === y || equals(x)(y);
export const getSetoidFn = <O extends Record<string,unknown>>(
eqs: { [K in keyof O]: SetoidFn<O[K]> },): SetoidFn<O> =>
setoidFnfrom(x => y =>
Object.entries<SetoidFn<unknonwn>>(eqs).every(([key,eq]) => eq(x[key]) (y[key]))
);
出现类型错误 TS(2345):Argument of type '{ [K in keyof O]: BinaryRelation<O[K]>; }' is not assignable to parameter of type '{ [s: string]: BinaryRelation<unknown>; } | ArrayLike<BinaryRelation<unknown>>'. Property 'length' is missing in type '{ [K in keyof O]: BinaryRelation<O[K]>; }' but required in type 'ArrayLike<BinaryRelation<unknown>>'.ts(2345)
但是当我尝试用 unknown
替换 any
就像
Object.entries<SetoidFn<any>>(eqs).every(([key,eq]) => eq(x[key]) (y[key]))
);
它有效。
我正在使用 typescript 4.1.3
我在这里遗漏了什么?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。