【问题标题】:Typescript compiler: Cannot find name 'Map'打字稿编译器:找不到名称“地图”
【发布时间】:2019-02-27 07:14:57
【问题描述】:

我有 Electron + Angular 应用程序。我想为 Electron 使用 Typescript,所以我有 main.ts 文件并想使用“tsc main.ts”将其编译为 main.js。但是,我收到以下错误:

node_modules/@types/selenium-webdriver/remote.d.ts:139:29 - 错误 TS2304:找不到名称“地图”。

仍然会生成 main.js,并且可以在我在没有 tsc 命令的情况下运行电子时使用。但是,我希望它由一个脚本运行而不会出现任何错误。

我的 tsconfig.json 包含:

{
 "compileOnSave": false,
 "compilerOptions": {
  "baseUrl": "./",
  "outDir": "./dist/out-tsc",
  "sourceMap": true,
  "declaration": false,
  "module": "es2015",
  "moduleResolution": "node",
  "emitDecoratorMetadata": true,
  "experimentalDecorators": true,
  "target": "es5",
  "typeRoots": [
    "node_modules/@types"
   ],
  "lib": [
   "es2017",
   "dom"
  ]
 }
}

我已经尝试过各种目标和 lib 配置组合(例如 es6),但没有成功。

有人可以帮忙吗? 非常感谢!

【问题讨论】:

  • 您的目标和库属性很好。这可能与typeRoots有关。也许该库取决于 nodejs 类型的版本或其他东西,并且由于多个版本而安装在子目录中

标签: angular typescript electron


【解决方案1】:

当您运行tsc main.ts 时,您的tsconfig.json 文件未被使用。而是运行tsc -p . 或简单地运行tsc,并在必要时使用tsconfig.json 中的filesincludeexclude 选项将输入文件限制为编译。

【讨论】:

  • 非常感谢马特,通过将命令更改为 tsc 并在 tsconfig.json 中的 compilerOptions 之前添加 includeexclude 部分来解决
  • @MichalPinka,我也遇到了同样的错误,你能给我看看你的 tsconfig.json 文件吗??
  • @METALHEAD { "compileOnSave": false, "include": [ "main.ts", "src/**/*" ], "exclude": [ "node_modules" ], "compilerOptions": { "baseUrl": "./", "outDir": "./dist/out-tsc", "sourceMap": true, "declaration": false, "module": "es2015", "moduleResolution": "node", "emitDecoratorMetadata": true, "experimentalDecorators": true, "target": "es5", "typeRoots": [ "node_modules/@types" ], "lib": [ "es2017", "dom" ] } }
【解决方案2】:

由于某种原因,当我像上面那样添加包含和排除部分时,仍然出现错误:

"../node_modules/@types/selenium-webdriver/http.d.ts:24:14 - error TS2583: Cannot find name 'Map'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later" 

运行时错误停止:npm i @types/node

我很想知道为什么(:

【讨论】:

  • npm i @types/node 也为我工作,你在文档中哪里找到这个,我想阅读更多
【解决方案3】:

从 Angular 6 升级到 Angular 8 后我遇到了这个问题。原来升级脚本更新了 Typescript 版本,但我的 package.json 中的 @types/node 包已过时(并且显然不再与新的 Typescript 兼容版本)。 更新它就行了!

【讨论】:

    【解决方案4】:

    我遇到了同样的问题。

    尝试执行以下命令:

    npm ci
    

    在我的电脑上修复了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 2016-09-15
      • 2019-02-25
      • 1970-01-01
      • 1970-01-01
      • 2017-07-29
      • 2016-01-28
      相关资源
      最近更新 更多