In node.js Javascript I can do this:
class MyClass{
data(){
return [
require('this-module'),
require('another-module),
require('that-module),
];
}
}
In Typescript I can write:
import * as ThisModule from 'this-module'
import * as AnotherModule from 'another-module'
import * as ThatModule from 'that-module'
class MyClass{
data():MyModuleTypeArray{
return [
ThisModule,
AnotherModule,
ThatModule
];
}
}
But in that solution I must write each name 3 times.
My question: Can I wrote the code in Typescript for inline include or in some other way in which I don't need to write 3 times the name (or to use an auxiliar variable)?
Of course I can write require but require returns any. I want the type checking.
The objective answer to your question is no, this is not possible. TypeScript /JavaScript currently does not provide a way to use import inline/synchronously in the same way require can be.
What you can do, though, is to call import dynamically as an async function. The direct implication of this change would be that your data method would have to be async.
The following would be the suggested implementation.
class MyClass{
async data() {
return Promise.all([
import('this-module'),
import('another-module'),
import('that-module'),
]);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With