Typescript error
(method) R.Static.prop(p: string): (obj: Record) => T (+3 overloads)
Returns a function that when supplied an object returns the indicated property of that object, if it exists.
Argument of type '(obj: Record) => T' is not assignable to parameter of type '(s: {}) => {}'. Types of parameters 'obj' and 's' are incompatible. Type '{}' is not assignable to type 'Record'. Index signature is missing in type '{}'.ts(2345)
The code:
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));
How this function is used
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}
In my actual app this is what the signature of my IAsset objects look like:
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));
}
}
However it still produces this Typescript warning:
Argument of type '(obj: Record) => T' is not assignable to parameter of type '(s: {}) => {}'. Types of parameters 'obj' and 's' are incompatible. Type '{}' is not assignable to type 'Record'. Index signature is missing in type '{}'.ts(2345)
Also side note, Typescript is killing the fun of beautiful small 1-line Ramda functional functions.