47

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

Ilja
  • 44,142
  • 92
  • 275
  • 498

2 Answers2

79

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.

kingdaro
  • 11,528
  • 3
  • 34
  • 38
  • 3
    It works for me using combination of `"module: "es7"` and `"moduleResolution": "node"` alongside the flag. There are issues with `@types/react` not being updated for this yet though. – Ilja Feb 14 '18 at 13:43
3

I have also faced the same issue,

  • This issue is specific to typescript version
  • Perform below steps to remove it ( It worked for me)
  1. In your project, inside package.json file - verify that you have typescript under the dependencies devDependencies{ "typescript": "^2.5.2" }
  2. check the installed version of the typescript on your system by typing tsc -version command in terminal
  • this will show the installed version of typescript at global level
  • o/p version - 2.9.1
  1. Change version number insider package.json file with the version installed on your system.

devDependencies{ "typescript": "^2.9.1" }

Hope this will work!!!!!!!!!!!!

atul parate
  • 760
  • 5
  • 5