Typescript:如何输入Ramda R.prop(key)T'不能赋值给'(s:{})=> {}类型的参数

问题描述 投票:2回答:1

打字稿错误

(方法)R.Static.prop(p:string):( obj:Record)=> T(+3重载)

返回一个函数,当提供对象时,返回该对象的指示属性(如果存在)。

类型'(obj:Record)=> T'的参数不能分配给'(s:{})=> {}'类型的参数。参数“obj”和“s”的类型不兼容。类型“{}”不能分配给“记录”类型。 “{}”类型中缺少索引签名.ts(2345)

enter image description here

代码:

https://ramdajs.com/docs/#lens

// Changes value of key in object without mutation
export const updateKey = (key: string) => R.lens(R.prop(key), R.assoc(key));

如何使用此功能

interface IMyObj {
  position: number;
  price: number;
  value: number;
}

const myObj: IMyObj = {
  position: 1,
  price: 100,
  value: 100
}

const updateKey = (key: string) => R.lens(R.prop(key), R.assoc(key));

const newObj = R.set(updateKey('value'), 200, myObj);

console.log('newObj', newObj); // {"position":1,"price":100,"value":200}

在我的实际应用程序中,这是我的IAsset对象的签名:

export interface IAsset {
  [key: string]: string | number | undefined | boolean;
  availableSupply?: string;
  currency: string;
  exchange: string;
  exchange_base?: string;
  marketCap: number;
  name: string;
  percentage?: number;
  price: number;
  position?: number;
  value?: number;
  inWatchlist?: boolean;
}
// Changes value of key in object without mutation
export const updateKey = (key: IAsset[string]) => {
  if (key) {
    return R.lens(R.prop(key), R.assoc(key));  
  }
}

但是它仍然会产生这个Typescript警告:

类型'(obj:Record)=> T'的参数不能分配给'(s:{})=> {}'类型的参数。参数“obj”和“s”的类型不兼容。类型“{}”不能分配给“记录”类型。 “{}”类型中缺少索引签名.ts(2345)

另外注意,Typescript正在扼杀美丽的小型1行Ramda功能的乐趣。

javascript typescript ramda.js
1个回答
2
投票

对于您的原始问题:

我不是Ramda的专家,所以也许我错过了使用这些类型的方法,但看起来你可以通过使updateKey泛型来摆脱类型错误:

const updateKey = <T, K extends string>(key: K) => R.lens(R.prop<K, T>(key), R.assoc(key));

请注意,此处的类型推断不会很好。如果将鼠标悬停在updateKey("value")上,则推断类型将为<{}, "value">(key: "value") => Lens,因此您可能需要在某些情况下明确指定类型参数。


对于您更新的问题:

肯定有一些我想念的东西。您的IAsset界面不支持仅数字键字符串。因此除了字符串之外不应该担心任何其他问题,但是,为了论证,我们假设您还想要处理数字或符号键。

如果你看一下@types/ramdapropassoc的单参数重载只接受字符串:

/**
 * Returns a function that when supplied an object returns the indicated property of that object, if it exists.
 */
prop<T>(__: Placeholder, obj: T): <P extends keyof T>(p: P) => T[P];
prop<P extends keyof T, T>(p: P, obj: T): T[P];
prop<P extends string>(p: P): <T>(obj: Record<P, T>) => T;
prop<P extends string, T>(p: P): (obj: Record<P, T>) => T;

/**
 * Makes a shallow clone of an object, setting or overriding the specified property with the given value.
 */
assoc<T, U>(__: Placeholder, val: T, obj: U): <K extends string>(prop: K) => Record<K, T> & U;
assoc<U, K extends string>(prop: K, __: Placeholder, obj: U): <T>(val: T) => Record<K, T> & U;
assoc<T, U, K extends string>(prop: K, val: T, obj: U): Record<K, T> & U;
assoc<T, K extends string>(prop: K, val: T): <U>(obj: U) => Record<K, T> & U;
assoc<K extends string>(prop: K): <T, U>(val: T, obj: U) => Record<K, T> & U;

我不确定,但我相信这是对@types/ramda项目的疏忽。您可以通过declaration merging以这种方式增强类型定义:

interface Static {
    prop<P extends keyof T, T>(p: P): (obj: Record<P, T>) => T;
    assoc<K extends keyof T, T>(prop: K): <T, U>(val: T, obj: U) => Record<K, T> & U;
}

然后像这样输入你的updateKey方法,而不需要单独的块来处理不同的键类型:

const updateKey = <K extends keyof IAsset>(key: K) => R.lens(R.prop<K, IAsset>(key), R.assoc<K, IAsset>(key));
© www.soinside.com 2019 - 2024. All rights reserved.