typescript 有没有办法在 tsconfig 中使用 --esModuleInterop 而不是一个标志?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48785451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to use --esModuleInterop in tsconfig as opposed to it being a flag?
提问by Ilja
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
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
Unless it somehow works with module?
除非它以某种方式与 module?
Main use case I want to achieve is to be able to import things like this
我想实现的主要用例是能够导入这样的东西
import React from "react"
import React from "react"
as opposed to
与
import * as React from "react"
import * as React from "react"
And do so from my tsconfig if possible
如果可能,请从我的 tsconfig 中执行此操作
回答by kingdaro
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": true
在您的 tsconfig.json 中执行。对于可以传递给 CLI 的每个标志选项,通常可以在配置文件中以这种方式完成相同的操作。这样做tsc --init
在命令行上生成一个tsconfig全面的注释解释所有可用的选项。
EDIT: I've learned that the behavior of esModuleInterop
is dependent on what is set to module
.
编辑:我了解到 的行为esModuleInterop
取决于设置为module
.
If you have "module": "commonjs"
, you only need to enable "esModuleInterop": true
.
如果有"module": "commonjs"
,您只需要启用"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.
如果您有"module": "es2015"
或"module": "esnext"
,您还必须启用"allowSyntheticDefaultImports": true
才能默认导入 CommonJS 模块(如 React)。
回答by atul parate
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 3) Change version number insider package.json file with the version installed on your system.
- 此问题特定于打字稿版本
- 执行以下步骤将其删除(它对我有用)1)在您的项目中,在 package.json 文件中 - 验证您在依赖项 devDependencies{ "typescript": "^2.5.2" } 2 下检查已安装的打字稿通过在终端中键入 tsc -version 命令,系统上的打字稿版本
- 这将在全局级别显示已安装的打字稿版本
- o/p 版本 - 2.9.1 3) 使用系统上安装的版本更改版本号 insider package.json 文件。
devDependencies{ "typescript": "^2.9.1" }
devDependencies{“打字稿”:“^2.9.1”}
Hope this will work!!!!!!!!!!!!
希望这会奏效!!!!!!!!!!!!