I currently have these types
:
type PossibleKeys = number | string | symbol;
type ValueOf<T extends object> = T[keyof T];
type ReplaceKeys<T extends Record<PossibleKeys, any>, U extends Partial<Record<keyof T, PossibleKeys>> =
Omit<T, keyof U> & { [P in ValueOf<U>]: T[keyof U] };
... however, while it is partially working, it's throwing the following error:
Type 'U[keyof U]' cannot be assigned to type 'string | number | symbol'.
interface Item {
readonly description: string;
readonly id: string;
}
interface MyInterface {
readonly id: string;
readonly propToReplace: number;
readonly anotherPropToReplace: readonly Item[];
}
type ReplacedUser = ReplaceKeys<MyInterface, { propToReplace: 'total', anotherPropToReplace: 'items' }>;
In the ReplacedUser
type, I can see that it is almost correct. The inferred type is:
{ id: string; total: number | readonly Item[]; items: number | readonly Item[]; }
However, I am expecting:
{ id: string; total: number; items: readonly Item[]; }
What am I doing incorrectly? I would like to understand how to specify that P
should receive the values passed in U
to prevent Typescript errors and then obtain the correct type for a specific value
.