Introduced in TypeScript 2.4 is support for the ECMAScript dynamic imports feature. If you didn’t see the announcement or read it properly, you’re probably here because you’re getting the following error.
In my case I use Webpack and I was trying to add in some dynamic import goodness and getting this error:
Dynamic import cannot be used when targeting ECMAScript 2015 modules.
You’re probably thinking, this is crazy considering dynamic imports are an ECMAScript feature, not a TypeScript one. The tell is in the error, if your
module is set to
es2015 you’re targeting ES6 and dynamic imports are a feature not due for release until ECMAScript 2018.
Funnily enough, the TypeScript team did reveal this in their official announcement but if you’re like me, you missed it the first time and hit this issue.
The fix is simply setting the
module value in your
tsconfig.json file to
esnext, like this:
"module": "esnext". If you’re using Visual Studio Code, you might get a squiggly in your
tsconfig.json file telling you it’s not a valid value, but ignore it because it is.