tsconfig - 编译 TypeScript 时找不到名称 'Symbol'
当我尝试编译一个 ts 文件时,收到以下错误:
node_modules/@types/node/util.d.ts(121,88): error TS2304: Cannot find name 'Symbol'.我查了一些资料,看到这可能与在 tsconfig.json 文件中没有声明正确的 target 或 lib 选项有关。我尝试了几种不同的方法,比如将 target 改为“es15”,并在 lib 中包含“es2015”,但我没有取得太大进展。
我正在使用这个教程作为我的项目基础:https://itnext.io/building-restful-web-apis-with-node-js-express-mongodb-and-typescript-part-1-2-195bdaf129cf
文件结构:
dist
lib
├──controllers
| ├──controller.ts
|
├──models
| ├──model.ts
|
├──routes
| ├──routes.ts
|
├──app.ts
├──server.ts
node_modules
package.json
tsconfig.json
tsconfig.json:
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"declaration": false,
"noImplicitAny": false,
"noImplicitThis": false,
"removeComments": true,
"experimentalDecorators": true,
"strictNullChecks": true,
"moduleResolution": "node",
"pretty": true,
"sourceMap": true,
"allowJs": true,
"noLib": false,
"jsx": "react",
"outDir": "./dist",
"lib": ["es2017"],
"baseUrl": "./lib"
},
"include": [
"lib/**/*.ts"
],
"exclude": [
"node_modules"
]
}
model.ts:
import * as mongodb from 'mongodb'
import * as fs from 'fs'
const filepath = __dirname + '/../file.txt'
function asyncReadFile(filepath: string, type: string) {
return new Promise((resolve, reject) => {
fs.readFile(filepath, (err, data) => {
console.log("Reading file...")
err ? reject(err) : resolve(data)
})
})
}
asyncReadFile(filepath, 'utf-8')
答案#1
我不确定 ES2017 是否可行,但是
我正在使用
"target":"es5"
并且 lib 最初在 tsconfig.ts 中
"lib":[]
仍然出现错误。
我在这个 GitHub 帖子上找到了解决方案,并且它有效。总之,
使用以下内容编辑你的 tsconfig.ts 文件
"lib": [
"es2015"
]
我的 Node 版本:8.11.2,npm 版本:5.6.0,仅供参考。
答案#2
看起来你没有为 es7 目标包含所有必需的库。如果你将目标降级到 es5 并移除 libs 选项,应该就可以了。
