I am struggling with implementing type checking in the register function. Currently, it accepts an array of Module[], which allows any options for a module. I want to set restrictions without using type assertion. Any advice on how to accomplish this would be greatly appreciated.
register([
{
use: NumberModule,
options: {
// I expected type checking to work without assertion
numb: 1
} as NumberModuleOptions // I want to eliminate this assertion
},
{
use: StringModule,
options: {
// I expected type checking to work without assertion
str: 'hello'
} as StringModuleOptions // I want to remove this
}
])
Full code
interface Module<Options, Contructor = ModuleContructor<Options> {
use: Contructor
options: Options
}
type ModuleContructor<Type> = new (...args: any[]) => ModuleInterface<Type>
interface ModuleInterface<Type> {
handle(data: Type): void
}
interface NumberModuleOptions {
numb: number
}
class NumberModule implements ModuleInterface<NumberModuleOptions> {
handle(data: NumberModuleOptions): void {
console.log(data)
}
}
interface StringModuleOptions {
str: string
}
class StringModule implements ModuleInterface<StringModuleOptions> {
handle(data: StringModuleOptions): void {
console.log(data)
}
}
function register(modules: Module<unknown>[]): void {
// some implementation
}