I'm trying to create a function that has a parameter thats an array of abstract classes and options to pass to their constructor:
class Provider<Options extends Record<string, unknown> = Record<string, unknown>> {
constructor(public readonly options: Options) {}
}
class ADF {
static create<Values extends Record<string, unknown>, P extends Provider>(providers?: [ProviderClass<P, P['options']>, P['options']][]) {
const adf = new ADF();
providers.forEach(([ClassName, options]) => {
adf.providers.push(new ClassName(options));
})
return adf;
}
}
I'm trying to use it like so:
const adf = ADF.create([
[FirstExampleProvider, { foo: 'bar' }],
[SecondExampleProvider, { bar: 'baz' }],
]);
The issue i'm having here is that any providers after the first one get compilation errors:
TS2322: Type 'typeof SecondExampleProvider' is not assignable to type 'ProviderClass<FirstExampleProvider, FirstExampleConfigValues>'. Types of construct signatures are incompatible. Type 'new (configuration: Configuration<SecondExampleConfigValues>, environment: EnvironmentProvider | undefined, options?: SecondExampleConfigValues) => SecondExampleProvider' is not assignable to type 'new (configuration: Configuration<FirstExampleConfigValues>, environment: EnvironmentProvider | undefined, options: FirstExampleConfigValues) => FirstExampleProvider'. Types of parameters 'configuration' and 'configuration' are incompatible. Type 'Configuration<FirstExampleConfigValues>' is not assignable to type 'Configuration<SecondExampleConfigValues>'. Property 'bar' is missing in type 'FirstExampleConfigValues' but required in type 'SecondExampleConfigValues'.
// And
TS2322: Type '{ bar: string; }' is not assignable to type 'FirstExampleConfigValues'. Object literal may only specify known properties, and 'bar' does not exist in type 'FirstExampleConfigValues'.
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.