I have a TypeScript application that utilizes es16 modules, with most being statically imported. I am now looking to incorporate a (validator) module that is only imported in debug mode. Everything seems to be functioning properly, but I am struggling to figure out how to type things to enable code completion and error-checking.
In my main class within main.ts, I have the following:
...
if(debug){
import('./validator.js').then((module) => this.validate(module))
}
...
The validate method is implemented as follows:
private validate(module):void{
new module.Validator(dataToValidate);
}
The content of validator.js includes:
export class Validator{
coonstructor(data:MyDatatype){
stuff going on here...
}
}
What I am seeking to understand/achieve is:
In the validate method:
private validate(module:someMeaningfulType){...}
Additionally, I would like to import the Validator class without actually importing it. If I were to write
import {Validator} from './validate.ts'
at the beginning of main.ts, I would load the file regardless of whether I need it, which contradicts the purpose of dynamic imports.
I could attempt to write a type declaration for module and Validator in main.ts, but even if there were no conflicts, I would still have to manually maintain synchronization with the actual module, which is not ideal - obviously.
I might be overlooking something obvious, but I am struggling to pinpoint it. Finding information solely on the use of native es2020/2022 modules with Typescript can be challenging due to an abundance of details about node-modules overshadowing it.