typescript webpack TS2304 找不到名称“地图”、“设置”、“承诺”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39416691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:19:54  来源:igfitidea点击:

webpack TS2304 Cannot find name 'Map', 'Set', 'Promise'

typescriptwebpack

提问by allenhwkim

I have the following webpack.config.js

我有以下 webpack.config.js

var path = require("path");
var webpack = require('webpack');

module.exports = {
  entry: {
    'ng2-auto-complete': path.join(__dirname, 'src', 'index.ts')
  },
  resolve: {
    extensions: ['', '.ts', '.js', '.json', '.css', '.html']
  },
  output: {
    path: path.join(__dirname, 'dist'),
    filename: "[name].umd.js",
    library: ["[name]"],
    libraryTarget: "umd"
  },
  externals: [
    /^rxjs\//,    //.... any other way? rx.umd.min.js does work?
    /^@angular\//
  ],
  devtool: 'source-map',
  module: {
    loaders: [
      { // Support for .ts files.
        test: /\.ts$/,
        loaders: ['ts', 'angular2-template-loader'],
        exclude: [/test/, /node_modules\/(?!(ng2-.+))/]
      }
    ]
  }
};

and the following tsconfig.json

和以下 tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "noEmitHelpers": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "sourceMap": true,
    "pretty": true,
    "allowUnreachableCode": true,
    "allowUnusedLabels": true,
    "noImplicitAny": false,
    "noImplicitReturns": false,
    "noImplicitUseStrict": false,
    "noFallthroughCasesInSwitch": false,
    "allowSyntheticDefaultImports": true,
    "suppressExcessPropertyErrors": true,
    "suppressImplicitAnyIndexErrors": true,
    "outDir": "dist",
    "baseUrl": "src"
  },
  "files": [
    "src/index.ts"
  ],
  "exclude": [
    "node_modules"
  ],
  "compileOnSave": false,
  "buildOnSave": false
}

When I run tsccommand as the following, it all works fine.

当我tsc按如下方式运行命令时,一切正常。

ng2-auto-complete (master)$ tsc --declaration
ng2-auto-complete (master)$ 

When I run webpackcommand, it shows typescript compile errors.

当我运行webpack命令时,它显示打字稿编译错误。

ng2-auto-complete (master)$ webpack
ts-loader: Using [email protected] and /Users/allen/github/ng2-auto-complete/tsconfig.json
Hash: bd6c50e4b9732c3ffa9d
Version: webpack 1.13.2
Time: 5041ms
                       Asset     Size  Chunks             Chunk Names
    ng2-auto-complete.umd.js  24.4 kB       0  [emitted]  ng2-auto-complete
ng2-auto-complete.umd.js.map  28.4 kB       0  [emitted]  ng2-auto-complete
    + 11 hidden modules

ERROR in /Users/allen/github/ng2-auto-complete/node_modules/@angular/platform-browser/src/dom/dom_renderer.d.ts
(18,37): error TS2304: Cannot find name 'Map'.

ERROR in /Users/allen/github/ng2-auto-complete/node_modules/@angular/platform-browser/src/dom/dom_adapter.d.ts
(96,42): error TS2304: Cannot find name 'Map'.

ERROR in /Users/allen/github/ng2-auto-complete/node_modules/@angular/platform-browser/src/web_workers/worker/location_providers.d.ts
(21,86): error TS2304: Cannot find name 'Promise'.
...
ng2-auto-complete (master)$ 

I don't know what I am missing for webpack and typescript compilation.

我不知道 webpack 和 typescript 编译缺少什么。

node_moduleshas been excluded in tsconfig.json

node_modules已被排除在 tsconfig.json

"exclude": [ "node_modules" ],

“排除”:[“节点模块”],

and type definitions are there in node_modules

和类型定义在 node_modules

  "devDependencies": {
    "@types/core-js": "^0.9.32",
    "@types/node": "^6.0.31"

I also tried to use typings.jsonand typings directory without success.

我也尝试使用typings.json和打字目录没有成功。

{
  "globalDependencies": {
    "core-js": "registry:dt/core-js#0.0.0+20160725163759",
    "jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
    "node": "registry:dt/node#6.0.0+20160815222444"
  }
}

FYI, versions

仅供参考,版本

$ node --version
v5.7.1
$ npm --version
3.6.0
$ tsc --version
Version 2.0.0

How do I get rid of TS2304 errors with webpackcommand?

如何使用webpack命令消除 TS2304 错误?

回答by allenhwkim

I added this to work in tsconfig.json, and it seems working without any error.

我在 中添加了tsconfig.json它,它似乎没有任何错误。

  "compilerOptions": {
    "target": "es5",
    "lib": ["es5", "es6", "dom"],  <--- this
    ...
  }

I am not sure libare for Typescript 2.0 function or not, but found out there are several libraries are available

我不确定lib是否适用于 Typescript 2.0 功能,但发现有几个库可用

From the typescript config schema (note the es2015.collection)

来自打字稿配置模式(注意 es2015.collection)

 "lib": {
      "description": "Specify library file to be included in the compilation. Requires TypeScript version 2.0 or later.",
      "type": "array",
      "items": {
        "type": "string",
        "enum": [ "es5", "es6", "es2015", "es7", "es2016", "es2017", "dom", "webworker", "scripthost", "es2015.core", "es2015.collection", "es2015.generator", "es2015.iterable",
                    "es2015.promise", "es2015.proxy", "es2015.reflect", "es2015.symbol", "es2015.symbol.wellknown", "es2016.array.include", "es2017.object", "es2017.sharedmemory" ]
      }
    }

This solves the compile errors, but I still wonder why tsccommand works without any errors, but webpackdoes not. tscsearches for all possible libraries without using libby tsconfig.json?

这解决了编译错误,但我仍然想知道为什么tsc命令可以正常工作而没有任何错误webpacktsc搜索所有可能的库而不使用libby tsconfig.json?

回答by Nitzan Tomer

Map, Setand Promiseare ES6features.
In your tsconfig.jsonyou are using:

MapSetPromiseES6特点。
在您tsconfig.json正在使用:

"target": "es5" 

This causes the compiler to use the normal es5lib.d.ts, which lacks the definitions for the above types.

这会导致编译器使用普通的es5lib.d.ts,它缺少上述类型的定义。

You want to use the lib.es6.d.ts:

你想使用lib.es6.d.ts

"target": "es6" 

回答by Legends

Just add:

只需添加:

 "lib": ["es6"] // means at least ES6

Don't change target. Target is used to tell Typescript into which version of ECMAScript to compile your .tsfiles. Of course, you can change it, if the browser your application will be running in, will support that version of ECMAScript.

不要改变目标。Target 用于告诉 Typescript 将您的.ts文件编译为哪个版本的 ECMAScript 。当然,您可以更改它,如果您的应用程序将在其中运行的浏览器支持该版本的 ECMAScript。

For example, I use "target": "es5"and "lib": ["es6"].

例如,我使用"target": "es5""lib": ["es6"]



Another reason could be:

另一个原因可能是:

That your .tsfile is not under "rootDir": "./YourFolder",

你的.ts文件不在"rootDir": "./YourFolder",

回答by user1618323

If you are wondering why none of these fixes workfor you keep in mind -- if you specify the file to compile on the command line or package.json tsc will NOT read your tsconfig.json file and therefore have no effect. Instead specify the "files" and "outDir" in your tsconfig.json and one of the "lib" fixes will probably work for you. Then compile with only:

如果您想知道为什么这些修复程序都不适合您,请记住——如果您在命令行或 package.json 上指定要编译的文件,tsc 将不会读取您的 tsconfig.json 文件,因此没有任何效果。而是在您的 tsconfig.json 中指定“files”和“outDir”,其中一个“lib”修复程序可能对您有用。然后只编译:

tsc --sourcemaps

tsc --sourcemaps

回答by phray2002

tsc index.ts --lib "es6"

If adding lib not working in tsconfig.json, using above command line option

如果在 tsconfig.json 中添加 lib 不起作用,请使用上面的命令行选项

回答by hannes neukermans

I had to install the core-js typings from npm to solve the issue

我不得不从 npm 安装 core-js 类型来解决这个问题

npm install @types/core-js

explanation:
The goal of @types npm packages is to obtain type definitions with npm. Using these type definitions is a TypeScript 2.0feature .

解释
@types npm 包的目标是使用 npm 获取类型定义。使用这些类型定义是TypeScript 2.0 的特性。

@types replacecurrent tools such as typingsand tsd, though these will continue to be supported for some time.

@types取代现有的工具,如分型TSD,虽然这些会持续一段时间的支持。

回答by lyrio

https://stackoverflow.com/a/44800490/9690407

https://stackoverflow.com/a/44800490/9690407

npm install typings -g
typings install

is deprecated in npm 5.6.0! Instead use the npm install @types/core-jssyntax.

在 npm 5.6.0 中已弃用!而是使用npm install @types/core-js语法。

回答by LeftOnTheMoon

Since the answer directly to the OP has been answered already, I figured I would add what fixed it for me. My situation was slightly different in that I was not using WebPack and was getting these errors when trying to use tsc. The answer everyone else is giving (add "es6" to lib) did not resolve it for me. The problem for me was that I had v9.11.1 of node installed on my machine, but I had used npm to grab "@types/node", which got the most recent, v10+. Once I uninstalled that node typing and installed a specific v9 node typing file, this issue was resolved.

由于已经直接回答了 OP,我想我会为我添加修复它的内容。我的情况略有不同,因为我没有使用 WebPack 并且在尝试使用 tsc 时遇到这些错误。其他人给出的答案(将“es6”添加到 lib)并没有为我解决。我的问题是我的机器上安装了 v9.11.1 的 node,但我使用 npm 来抓取“@types/node”,它得到了最新的 v10+。一旦我卸载了那个节点类型并安装了一个特定的 v9 节点类型文件,这个问题就解决了。

回答by Andrew Odiit

I'm using node.js v10.16.3.The problem for me was that the typescript compiler was ignoring my tsconfig.jsonfile.

我正在使用node.js v10.16.3.对我来说问题是打字稿编译器忽略了我的tsconfig.json文件。

Three solutions worked for me:

三种解决方案对我有用:

  1. Install ts-node and use that instead to compile and run the file
  2. Do tsc filename.ts --lib "es6", "dom"when you compile the file
  3. Install @types/nodewhich will allow you to run tsc filename.tswithout errors.
  1. 安装 ts-node 并使用它来编译和运行文件
  2. 不要tsc filename.ts --lib "es6", "dom"在编译文件
  3. 安装@types/node这将使您可以tsc filename.ts无错误地运行。

回答by bhoopal janga

In your tsconfig.json just change "target": "es5" to "target": "es6"

在您的 tsconfig.json 中,只需将 "target": "es5" 更改为 "target": "es6"