My goal is to pass an array of different types to generate various combinations of submodules.
However, I am currently only passing a single type which works fine. When I try to pass multiple types, the compilation fails.
What steps can I take to resolve this issue?
export enum Module {
'Video' = 0,
'Chat',
}
export interface ModulesMaps {
[Module.Video]: VideoModule;
[Module.Chat]: ChatModule;
}
export interface VideoModule {
getVideoModule(): string;
}
export interface ChatModule {
getChatModule(): string;
}
export interface CommonModule {
init(): void;
}
export type Core<T extends Module> = CommonModule & ModulesMaps[T]
export function createClient<T extends Module>(modules: T[]): Core<T>{
// fake code
return undefined as unknown as Core<T>;
}
let client1 = createClient([Module.Video]);
client1.getVideoModule();
client1.init();
let client2 = createClient([Module.Chat]);
client2.getChatModule();
client2.init();
let client3 = createClient([ Module.Chat | Module.Video ]);
client3.getVideoModule(); //compile error
client3.getChatModule(); //compile error
client3.init();
Playground : typescriptlang.org playground
I want to pass in an array of different types so that I can get different combinations of submodules.
But I pass in a single type which is fine, and when I pass in multiple types, it compiles incorrectly.
How do I change this?