Imports must be top level
That is, they must be in the main sequence of instructions that is performed when a module is executed. There is no way to conditionally import something.
Loading TypeScript...
This also applies to functions and loops.
Loading TypeScript...
If you need to load something dynamically, use the import
function, which returns a Promise
.
Loading TypeScript...
Why?
The TypeScript compiler, along with bundlers like Webpack or ESBuild, rely on static analysis - the process of analyzing a program's code to determine dependencies and optimizations.