Typescript v 2.7 released really neat flag called --esModuleInterop
https://www.typescriptlang.org/docs/handbook/compiler-options.html, I am trying to figure out if there is a way to use it with tsconfig.json
as currently it doesn't seem to be documented : http://www.typescriptlang.org/docs/handbook/tsconfig-json.html
Unless it somehow works with module?
Main use case I want to achieve is to be able to import things like this
import React from "react"
as opposed to
import * as React from "react"
And do so from my tsconfig if possible
The error "Module can only be default-imported using esModuleInterop flag" occurs when we try to import a CommonJS module into an ES6 module. To solve the error, set the esModuleInterop option to true in your tsconfig. json file.
We highly recommend applying it both to new and existing projects. esModuleInterop is also a recommended option to set in tsconfig. json and when one runs tsc --init it gets set to true automatically.
esModuleInterop. Enable TypeScript emitted code to work with code emitted by the Babel ecosystem. This flag is introduced in TypeScript 2.7. Prior to 2.7, when importing commonjs module, you have to do either import x = require('x') or import * as x from 'x' .
When set to true, allowSyntheticDefaultImports allows you to write an import like: ts. import React from "react"; instead of: ts.
Yes, do "esModuleInterop": true
in your tsconfig.json. For every flag option that can be passed to the CLI, the same can usually be done this way in the config file. Doing tsc --init
on the command line generates a tsconfig full of comments explaining all of the available options.
EDIT: I've learned that the behavior of esModuleInterop
is dependent on what is set to module
.
If you have "module": "commonjs"
, you only need to enable "esModuleInterop": true
.
If you have "module": "es2015"
or "module": "esnext"
, you also have to enable "allowSyntheticDefaultImports": true
in order to import CommonJS modules (like React) as a default.
I have also faced the same issue,
devDependencies{ "typescript": "^2.9.1" }
Hope this will work!!!!!!!!!!!!
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