I currently have these types, which works great:
type ConfigEntry<T> = {
value: T;
option1: boolean;
option2: number;
option3: string;
// ...
};
type Config<T> = Partial<{
[U in keyof T]: ConfigEntry<T[U]>;
}>;
However, in my code I want to be able to loop through the entries of Config
using Object.entries()
. The problem is that Object.entries()
casts the keys to a string
, so it loses the type safety. Therefore I want to be able to cast them to their corresponding type.
I have tried using the following in a generic class of type T:
const entries = Object.entries(configObj) as [keyof T, ConfigEntry<keyof T>][];
Unfortunately, this does not ensure that the key (first index) is actually of the same type as the value (second index), but rather any of the value types defined in type T.
How do I solve this in TypeScript?