Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
931 views
in Technique[技术] by (71.8m points)

typescript - Is there a way to use --esModuleInterop in tsconfig as opposed to it being a flag?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...