What would be the best way of achieving this effect (note: this is not valid syntax - consider it a pseudocode):
type Config = {
[key: string]: <T> {
params: T,
doSth: (params: T) => void,
},
}
I.e. the generic T
type is different for every key's value (which is an object itself), but at the same time it's being reused within that value (object) under different fields. Also, T
is derived from the params
field. With that I could do:
const config: Config = {
a: {
params: { x: 123 },
doSth: params => {}, // Type of "params" is "{ x: 123 }"
},
b: {
params: { y: 'asd' },
doSth: params => {}, // Type of "params" is "{ y: 'asd' }"
},
}
To my surprise, googling this doesn't yield matching results, even though it seems relatively useful pattern - there's a lot of similar problems but not really the same. Some solutions to those, that I tried to apply - all failed.