Currently I have the following 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] };
...but, although it works even partially, it gives the following error:
Type 'U[keyof U]' is not assignable 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 ReplacedUser I can see that the type is almost correct. The inferred type is:
{ id: string; total: number | readonly Item[]; items: number | readonly Item[]; }
... while I'm expecting:
{ id: string; total: number; items: readonly Item[]; }
What am I doing wrong? I'd like to know first how I can express that P needs to get the values passed in U to suppress Typescript errors and after that get the correct type for a specific value.