I have a system that takes key:object pairs and then lets you get them later in the application.
The problem is having this index signature on breaks the typing because it allows any key at all to exist so you can't type the get() requests.
A summarized snippet of the application and problem is below.
I have tried removing this index signature which is a problem because then the setting[key].value
in class Configuration
does not exist. I have tried removing the base Settings interface but that basically breaks any change you have of typing things. I have tried quite a few helper functions and guards to try to force the application to realize that it is a setting. I am not at the point where I will have to accept no typing on the get method.
class Setting {
constructor(
public value: number,
private locked: boolean
) { }
}
class Settings {
[key: string]: Setting; // Comment me out: errors (but typing), leave me in: no errors (no typing)
}
interface SettingsCountryRank extends Settings {
taiwan: Setting;
china: Setting;
}
class Configuration<TSettings extends Settings> {
settings: TSettings;
public get<U extends keyof TSettings>(key: U): TSettings[U]['value'] {
return this.settings[key].value;
}
constructor(settings: TSettings) {
this.settings = settings;
}
}
function main(){
const test = new Configuration<SettingsCountryRank>({ taiwan: new Setting(1, true), china: new Setting(2, false) });
test.get('china')
test.get('notathing');
}
When you toggle on and off the line in Settings
you can see the problem.