webpack TS2304找不到名称'Map','Set','Promise'

all*_*kim 66 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', '.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-.+))/]
      }
    ]
  }
};
Run Code Online (Sandbox Code Playgroud)

以及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
}
Run Code Online (Sandbox Code Playgroud)

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

ng2-auto-complete (master)$ tsc --declaration
ng2-auto-complete (master)$ 
Run Code Online (Sandbox Code Playgroud)

当我运行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)$ 
Run Code Online (Sandbox Code Playgroud)

我不知道webpack和打字稿编译我缺少什么.

node_modules 被排除在外 tsconfig.json

"exclude":["node_modules"],

有类型定义 node_modules

  "devDependencies": {
    "@types/core-js": "^0.9.32",
    "@types/node": "^6.0.31"
Run Code Online (Sandbox Code Playgroud)

我也尝试使用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"
  }
}
Run Code Online (Sandbox Code Playgroud)

仅供参考,版本

$ node --version
v5.7.1
$ npm --version
3.6.0
$ tsc --version
Version 2.0.0
Run Code Online (Sandbox Code Playgroud)

如何通过webpack命令摆脱TS2304错误?

all*_*kim 113

我添加了这个工作tsconfig.json,它似乎工作没有任何错误.

  "compilerOptions": {
    "target": "es5",
    "lib": ["es5", "es6", "dom"],  <--- this
    ...
  }
Run Code Online (Sandbox Code Playgroud)

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

来自typescript配置架构(注意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" ]
      }
    }
Run Code Online (Sandbox Code Playgroud)

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

  • 你应该选择你的答案作为首选解决方案 (4认同)

Nit*_*mer 44

Map,SetPromiseES6特点.
tsconfig.json你的使用中:

"target": "es5" 
Run Code Online (Sandbox Code Playgroud)

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

你想使用lib.es6.d.ts:

"target": "es6" 
Run Code Online (Sandbox Code Playgroud)

  • 然后,如果我们想要满足IE11,`es6`对象; Map,Promise,应转换为`es5`对象.因此,目标应该是'es5`而不是'es6`.我对吗? (8认同)
  • 它不是拾取库的"目标",但"lib"应包括这些对象的库.看看@allenhwkim的回答.这解决了问题,是正确的答案. (4认同)

Leg*_*nds 37

不要改变任何东西,只需添加:

 "lib": ["es6"] // means at least ES6
Run Code Online (Sandbox Code Playgroud)

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

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


另一个原因可能是:

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


use*_*323 11

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

tsc --sourcemaps


小智 10

tsc index.ts --lib "es6"
Run Code Online (Sandbox Code Playgroud)

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


han*_*ans 7

我必须从npm安装core-js打字来解决问题

npm install @types/core-js
Run Code Online (Sandbox Code Playgroud)

解释:
@types npm包的目标是使用npm获取类型定义.使用这些类型定义是TypeScript 2.0功能.

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