【发布时间】:2019-10-07 20:27:03
【问题描述】:
我正在查看某人 .tsconfig 的文件,并在那里发现了 --esModuleInterop
这是他的.tsconfig文件
{
"compilerOptions": {
"moduleResolution": "node",
"target": "es6",
"module": "commonjs",
"lib": ["esnext"],
"strict": true,
"sourceMap": true,
"declaration": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"declarationDir": "./dist",
"outDir": "./dist",
"typeRoots": ["node_modules/@types"]
},
"include": ["src/**/*.ts"],
"exclude": ["node_modues"]
}
在这里,我的主要问题是 "esModuleInterop": true, 和
"allowSyntheticDefaultImports": true,。我知道他们有点依赖 "module": "commonjs", 。有人可以尝试用最好的人类语言来解释吗?
allowSyntheticDefaultImports states 的官方文档
允许从没有默认导出的模块进行默认导入。这确实 不影响代码发出,只是类型检查。
这是什么意思?如果没有任何导出默认值,那么我认为导入默认值的唯一用例是初始化某些东西?像单身?
以下问题/答案也没有意义 Is there a way to use --esModuleInterop in tsconfig as opposed to it being a flag?
和--esModuleInterop编译页面上的定义
为运行时 babel 发出 __importStar 和 __importDefault 助手 生态系统兼容性并启用 --allowSyntheticDefaultImports for 类型系统兼容性。
对我来说似乎也很难理解/理解
【问题讨论】:
-
我会在 NodeJS v16 中给你一个实际的例子。如果您的 npm 包 A 导入了“集群”,并且您的项目依赖于您的 npm 包 A 并从中导入了一个类,那么在您的 npm 包 A 中没有将“esModuleInterop”设置为 true,则在 npm 包 A 中未定义集群,但是项目本身完全没问题。一直以来,一切都很好地编译,没有任何警告。
标签: typescript