I'm having a bit of trouble getting they type assertions I'd like to have.
I have a function
const setSetting: SetSettingFn = (key, value) => {
if (typeof settings[key] != null) {
settings = {
...settings,
[key]: value
}
}
}
with the SetSettingFn type of:
type SetSettingFn = (key: keyof AppSettings, value: AppSettings[keyof AppSettings]) => void
I'd like the value prop to know, based on the key that was entered the proper type to assert from that key. The above just kind of mashes all the types in AppSettings into one flat type with everything.
For example if settings.foo is boolean, then this should throw a type error:
setSetting('foo', 1)
settings[key] = valueinstead ofsettings = {...settings, [key]: value}?typeof AppSettings[keyof AppSettings]?