问题描述
Typescript v 2.7 发布了名为 --esModuleInterop
的非常简洁的标志 https://www.typescriptlang.org/docs/handbook/compiler-options.html,我想知道是否有办法将它与 tsconfig.json
目前似乎没有记录:http://www.typescriptlang.org/docs/handbook/tsconfig-json.html
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
除非它以某种方式与 module 一起工作?
我想实现的主要用例是能够导入这样的东西
Main use case I want to achieve is to be able to import things like this
从react"导入 React
相对于
import * as React from "react"
如果可能的话,从我的 tsconfig 中这样做
And do so from my tsconfig if possible
推荐答案
是的,在 tsconfig.json 中执行 "esModuleInterop": true
.对于可以传递给 CLI 的每个标志选项,通常可以在配置文件中以这种方式完成相同的操作.在命令行上执行 tsc --init
会生成一个 tsconfig,其中包含解释所有可用选项的注释.
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.
我了解到 esModuleInterop
的行为取决于设置为 module
的内容.
I've learned that the behavior of esModuleInterop
is dependent on what is set to module
.
如果你有"module": "commonjs"
,你只需要启用"esModuleInterop": true
.
If you have "module": "commonjs"
, you only need to enable "esModuleInterop": true
.
如果你有 "module": "es2015"
或 "module": "esnext"
,你还必须启用 "allowSyntheticDefaultImports": true
以便默认导入 CommonJS 模块(如 React).
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.
这篇关于有没有办法在 tsconfig 中使用 --esModuleInterop 而不是一个标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!