What I have?
The object of settings like this:
const settings = {
groupOne: {
settingOne: 'string',
settingTo: 123,
},
groupTwo: {
settingThree: true,
},
};
What I want?
Function (and its type) that gets a group key and a setting key as parameters and returns value of it.
type GetSettingFunction = (group: GroupKey, setting: SettingKey<typeof group>)
=> Setting<typeof group, typeof setting>;
const getSetting: GetSettingFunction = (group, setting) => {
// some code here
}
I have implemented some intermediate types and it seems working
type Settings = typeof settings;
type GroupKey = keyof Settings;
type Group<T extends GroupKey> = {
[K in GroupKey]: T extends K ? Settings[T] : never;
}[T];
type SettingsGroup<T extends GroupKey> = {
[K in GroupKey]: K extends T ? Group<T> : never;
}[T];
type SettingKey<T extends GroupKey> = {
[K in GroupKey]: K extends T ? keyof SettingsGroup<K>: never;
}[T];
But I can not understand how to implement final type Setting<GroupKey, SettingKey>. Can you help me, please?