尝试编译 ts 文件时收到以下错误:
node_modules/@types/node/util.d.ts(121,88):错误 TS2304:找不到名称“符号”。
我做了一些阅读,发现这可能与 tsconfig.json
文件中没有声明正确的目标或 lib 选项有关。我尝试了一些不同的方法,例如将目标更改为“es15”并在库中包含“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"
]
}
模型.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')
最佳答案
我不确定 es2017 是否有效,但是
我正在使用
"target":"es5"
lib 最初位于 tsconfig.ts
"lib":[]
仍然出现错误。
我在这个github上找到了解决方案post它起作用了。综上所述,
使用
编辑 tsconfig.ts 文件 "lib": [
"es2015"
]
我的节点版本:8.11.2 和 npm 版本:5.6.0,如果有帮助的话。
关于typescript - 编译 typescript 时找不到名称 'Symbol',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55760457/