Typescript 网页包TS2304找不到名称';地图'';设置'';承诺';

Typescript 网页包TS2304找不到名称';地图'';设置'';承诺';,typescript,webpack,Typescript,Webpack,我有以下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',

我有以下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-.+))/]
      }
    ]
  }
};
以及下面的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
}
当我按如下方式运行
tsc
命令时,一切正常

ng2-auto-complete (master)$ tsc --declaration
ng2-auto-complete (master)$ 
当我运行
webpack
命令时,它显示typescript编译错误

ng2-auto-complete (master)$ webpack
ts-loader: Using typescript@2.0.0 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)$ 
我不知道我在网页包和打字脚本编译中缺少了什么

tsconfig.json

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

和类型定义在
node\u模块中

  "devDependencies": {
    "@types/core-js": "^0.9.32",
    "@types/node": "^6.0.31"
我还尝试使用
typings.json
和typings目录,但没有成功

{
  "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"
  }
}
供参考,版本

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

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

Map
Set
Promise
ES6
的功能。
tsconfig.json
中,您正在使用:

"target": "es5" 
这导致编译器使用normal,它缺少上述类型的定义

您要使用以下命令:


我将其添加到
tsconfig.json
中,它似乎可以正常工作

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

这解决了编译错误,但我仍然想知道为什么
tsc
命令工作时没有任何错误,而
webpack
没有
tsconfig.json
通过
tsconfig.json

在tsconfig.json中搜索所有可能的库,只需将“target”:“es5”更改为“target”:“es6”

我必须从npm安装核心js打字来解决这个问题

npm install @types/core-js
解释
@types-npm包的目标是使用npm获得类型定义。使用这些类型定义是TypeScript 2.0的一项功能


@类型替换当前的工具,如打字tsd,尽管这些工具将在一段时间内继续得到支持。

在我的情况下,我必须运行:

npm install typings -g
typings install
这解决了我的问题。

只需添加:

 "lib": ["es6"] // means at least ES6
不要改变目标。 目标用于告诉Typescript将
.ts
文件编译成哪个版本的ECMAScript。当然,如果运行应用程序的浏览器支持该版本的ECMAScript,则可以对其进行更改

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


另一个原因可能是:

您的
.ts
文件不在
的“rootDir”:“/YourFolder”下,

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

tsc index.ts --lib "es6"

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

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

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


tsc--sourcemaps

要解决此错误,请更改tsconfig.json文件中的以下属性

"lib": [
      "es2018",
      "dom",
      "es5", 
      "es6"
    ],
"module": "es2015",
"target": "es6"
之后,在终端中运行以下命令

npm install @types/es6-shim
已解决错误。

对于es6,请使用此选项

tsc filename.ts --lib es2015

我使用的是
node.jsv10.16.3。
问题是typescript编译器忽略了我的
tsconfig.json
文件

三种解决方案对我有效:

  • 安装ts节点并使用它编译和运行文件
  • 编译文件时,请执行tsc filename.ts--lib“es6”、“dom”
  • 安装
    @types/node
    ,它将允许您无错误地运行
    tsc filename.ts

  • 要解决此问题,只需在ts文件中导入map方法,如下所示:

    import { map } from 'rxjs/operators';
    

    对我来说,解决方案是安装
    @types/node

    纱线添加@types/node--dev
    
    或者,如果您更喜欢npm:

    npm安装@types/node--dev
    

    但是,我假设如果您打算继续使用“Map”、“Set”或“Promise”,那么最好在
    tsconfig.json
    中的“lib”数组中包含“es6”。

    基于,目标应该是“es5”,以生成
    es5
    代码。如果目标是
    es6
    ,它是否仍能与较低的浏览器一起工作,例如“IE11”?否,并且并非所有浏览器都支持最新的es6功能。但是如果您想使用es6功能,如
    Promise
    Map
    ,那么您需要以es6为目标,或者从
    libe.es6.d.ts
    中复制所需的定义,然后,如果我们想满足IE11,
    es6
    对象;Map、Promise应转换为
    es5
    对象。因此,目标应该是
    es5
    而不是
    es6
    。我说的对吗?不是“目标”获取库,而是“库”应该包含这些对象的库。看看@allenhwkim的答案。这就解决了问题,这是正确的答案。我的观点是“target”是指定输出类型应该是什么(对于OP是es5),而“lib”是指定webpack编译源代码时应该使用的库。您的回答/评论听起来好像“目标”是指定LIB。请注意,自从TypeScript 2.0以来,打字已不推荐使用。请改用@types。您应该选择您的答案作为首选解决方案谢谢我正在搜索
    npm install @types/es6-shim
    
    tsc filename.ts --lib es2015
    
    import { map } from 'rxjs/operators';