我正在检查某人的.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"]
}
Run Code Online (Sandbox Code Playgroud)
在这里,我的主要问题是 "esModuleInterop": true,和
"allowSyntheticDefaultImports": true,。我知道他们有点依赖 "module": "commonjs",。有人可以尝试用最好的人类语言来解释它吗?
allowSyntheticDefaultImports州的官方文档
允许从模块进行默认导入而没有默认导出。这不影响代码发出,仅影响类型检查。
那是什么意思?如果没有任何导出默认值,我认为导入默认值的唯一用例就是初始化某些东西?喜欢单身人士吗?
以下问题/答案也没有意义, 是否可以在tsconfig中使用--esModuleInterop而不是将其作为标志?
并--esModuleInterop在编译器页面上定义
发出__importStar和__importDefault帮助程序以实现运行时babel生态系统兼容性,并启用--allowSyntheticDefaultImports以实现类型系统兼容性。
我似乎也很难理解/理解
我的错误是:
Error: src/app.ts(11,13): error TS2349: This expression is not callable.
Type 'typeof import("express")' has no call signatures.
Run Code Online (Sandbox Code Playgroud)
我的tsconfig.json是:
{
"compilerOptions": {
"outDir": "./built",
"allowJs": true,
"target": "es6",
"esModuleInterop": true
},
"include": [
"./src/**/*"
]
}
Run Code Online (Sandbox Code Playgroud)
我的src/app.ts有:
// const Logger = require('./lib/logger')
import express from 'express';
import bodyParser from 'body-parser';
// const finale = require('finale-rest')
// const morgan = require('morgan')
const DB = require('./models')()
// const resources = require('./resources')
const app = express()
Run Code Online (Sandbox Code Playgroud)
有问题的线路是 const app = …