typescript - 编译 typescript 时找不到名称 'Symbol'

标签 typescript tsconfig

尝试编译 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/

相关文章:

javascript - console.log 返回待处理的 promise ,而不是实际数据

angular - 使用 Angular 向 Fabric.js 对象添加自定义属性

Angular2 导入绝对路径或自定义路径(typescript 2)

javascript - Typescript 库到单个全局 javascript 文件

javascript - 带有来自 Typescript 的路径的 Monorepo 不起作用

javascript - 使用 requirejs 加载一个文件,里面有几个类(grunt-concat)

typescript - 为什么 Typescript 会推断由没有未定义的缺失键检索到的值的类型?

javascript - 如何以 Angular 将 JSON 传递给@Input?

typescript - 设置 tsconfig 忽略 "Property ' foo' does not exist on type 'bar' ."but only for a specific variable

javascript - 当下一个js发生错误时,Typescript编译器不会停止