Apologies for my limited English skills, but I will do my best to explain my dilemma.
I am looking to create a TypeScript function that can replace the keys of an Object.
For example:
interface Target {
name: string;
ID: number;
}
// The function
function objectAdapter<T extends Object, K extends keyof T>(
data: T,
transformMap: Record<keyof Target, K>
) {
return Object.keys(transformMap).reduce((p, c) => (p[c] = data[transformMap[c]], p), {})
}
const result = objectAdapter({ nickname: 'link', id: 123456 }, { name: 'nickname', id: 'ID' });
The code above allows me to achieve the following transformation:
// Transforming data from
{
nickname: 'link',
id: 123456
}
// To
{
name: 'link',
ID: 123456
}
However, when filling in the values of `transformMap`, my IDE did not provide correct suggestions.
Moreover, all properties of the `result` are inferred as 'string|number', whereas I would like them to be similar to the `Target` interface.
Thank you for reading and anticipating your support. Thank you♪(・ω・)ノ.