How can we implement this specific pattern in code (please note that this is not valid syntax and should be considered pseudocode):
type Config = {
[key: string]: <T> {
params: T,
doSth: (params: T) => void,
},
}
In this scenario, the generic type T
is unique for each key's value within the object, but it is also reused under different fields within the same value. Additionally, the T
type is derived from the params
field. This setup allows for:
const config: Config = {
a: {
params: { x: 123 },
doSth: params => {}, // The type of "params" would be "{ x: 123 }"
},
b: {
params: { y: 'asd' },
doSth: params => {}, // The type of "params" would be "{ y: 'asd' }"
},
}
Surprisingly, searching for similar implementations or solutions to this problem did not yield relevant results. It appears to be a useful pattern with no direct matches in existing resources. Previous attempts to adapt solutions of similar problems have been unsuccessful.