编译打字稿时找不到名称'Symbol'

问题描述 投票:1回答:2

我在尝试编译ts文件时收到以下错误:

node_modules/@types/node/util.d.ts(121,88): error TS2304: Cannot find name 'Symbol'.

我做了一些阅读,发现这可以链接到没有在tsconfig.json文件中声明的正确目标或lib选项。我尝试了一些不同的东西,比如将目标更改为“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')
typescript tsconfig
2个回答
0
投票

您似乎没有包含es7目标所需的所有库。如果将目标降级到es5并删除libs选项,那么你应该很好。


0
投票

我不确定es2017会不会起作用,但是

我在用

"target":"es5"

和lib最初是在tsconfig.ts中

"lib":[]

并仍然得到错误。

我在这个github post找到了解决方案并且它有效。综上所述,

用。编辑你的tsconfig.ts文件

 "lib": [
  "es2015"
]

我的节点版本:8.11.2和npm版本:5.6.0,如果有帮助的话。

© www.soinside.com 2019 - 2024. All rights reserved.