I'm creating a generic class like this:
type Config = {
value: string
}
class MyClass<T> {
configs:{[key in keyof T]: Config },
data: {[key in keyof T]?: string}
constructor(configs: { [key in keyof T]: Config }) {
this.configs = configs;
this.data= {};
}
init() {
Object.entries(this.configs).forEach(([configName, config]) => {
if (!(configName in this.data)) {
this.data[configName] = dosomething(config); // dosomething returns string
}
});
}
}
Then use the class like:
const cls = MyClass({
cfg1: {value: 'value1'},
cfg2: {value: 'value2'},
})
cls.init();
And the cls.data is expected to be:
{
cfg1: 'calculated1',
cfg2: 'calculated2',
}
But in the forEach, configName is string and config is unknown.
I tried ([configName, config]: [key in keyof T, Config]) => but that's not working.
How can I do this?