Take a look at the code snippet provided. The concept here is that I have different provider implementations that extend a base provider. Each provider requires a settings object that is an extension of a base settings object. Additionally, each provider contains a static method to test these settings before they are passed to the provider (this method must remain static due to legacy reasons and cannot be changed to an instance method at this time)
enum ProviderType {
TypeA = 'typeA',
TypeB = 'typeB',
}
interface Settings {
commonProperty: string;
}
interface SettingsTypeA extends Settings {
propertyA: string;
}
interface SettingsTypeB extends Settings {
propertyB: string;
}
type SettingsMap = {
[ProviderType.TypeA]: SettingsTypeA,
[ProviderType.TypeB]: SettingsTypeB,
}
interface TestSettingsOptions<T extends ProviderType> {
settings: SettingsMap[T];
}
abstract class BaseProvider<T extends ProviderType> {
constructor(protected settings: SettingsMap[T]) {}
static testSettings<T extends ProviderType>(opts: TestSettingsOptions<T>) {
throw new Error('Method not implemented');
}
}
class ProviderA extends BaseProvider<ProviderType.TypeA> {
constructor(protected settings: SettingsTypeA) {
super(settings); // Settings has the correct type here: SettingsTypeA
}
static testSettings(opts: TestSettingsOptions<ProviderType.TypeA>) {
// do some testing
}
}
class ProviderB extends BaseProvider<ProviderType.TypeB> {
constructor(protected settings: SettingsTypeB) {
super(settings); // Settings has the correct type here: SettingsTypeB
}
static testSettings(opts: TestSettingsOptions<ProviderType.TypeB>) {
// do some testing
}
}
Although the basic classes, interfaces, and mapped types are inferred correctly, there seem to be issues with the static methods. Refer to the image below for reference:
https://i.sstatic.net/wyHA7.png
I'm uncertain about what mistake I might be making or why TypeScript is rejecting it as a valid type. Can someone provide guidance on this matter?