【问题标题】:TypeScript cannot Find name Weakmap, Map, etcTypeScript 找不到名称 Weakmap、Map 等
【发布时间】:2018-12-10 13:54:22
【问题描述】:

我想到了 Angular 教程,并有这个 ts 和 web pack 配置

这里是 tsconfig

{
"compilerOptions": {
  "target": "es5",
  "module": "es2015",
  "moduleResolution": "node",
  "sourceMap": true,
  "emitDecoratorMetadata": true,
  "experimentalDecorators": true,
  "lib": ["es6", "dom"],
  "noImplicitAny": true,
  "suppressImplicitAnyIndexErrors": true,
  "typeRoots": [
    "node_modules/@types/"
  ]
},
"exclude": [
    "node_modules"
]

}

这里是 webpack.config.js

    var path = require('path');
var webpack = require('webpack');
var UglifyJSPlugin = require('uglifyjs-webpack-plugin'); 
module.exports = {
    entry: {
        'polyfills': './src/polyfills.ts',
        'app': './src/main.ts'
      },
   output:{
       path: path.resolve(__dirname, './public'),    
       publicPath: '/public/',
       filename: "[name].js"      
   },
   resolve: {
    extensions: ['.ts', '.js']
  },
   module:{
       rules:[   
           {
               test: /\.ts$/, 
               use: [
                {
                    loader: 'awesome-typescript-loader',
                    options: { configFileName: path.resolve(__dirname, 'tsconfig.json') }
                  } ,
                   'angular2-template-loader'
               ]
            }
       ]
   },
   plugins: [
    new webpack.ContextReplacementPlugin(
        /angular(\\|\/)core/,
        path.resolve(__dirname, 'src'), 
      {} 
    ),
    new UglifyJSPlugin()
  ]
}

这是我的项目根源

Screen

但是当我运行项目时,我得到了这些错误。

[at-loader] 中的错误 ./node_modules/@types/node/index.d.ts:6208:55 TS2304:找不到名称“地图”。

[at-loader] 中的错误 ./node_modules/@types/node/index.d.ts:6215:55 TS2304:找不到名称“Set”。

[at-loader] 中的错误 ./node_modules/@types/node/index.d.ts:6225:59 TS2304:找不到名称“WeakMap”。

[at-loader] 中的错误 ./node_modules/@types/node/index.d.ts:6226:59 TS2304:找不到名称“WeakSet”。

这是来自终端的完整日志

https://pastebin.com/jyRLSn42

我该如何解决这个问题?

【问题讨论】:

    标签: angular typescript webpack


    【解决方案1】:

    安装 corejs 类型

    npm install --save-dev @types/core-js

    也将其添加到 tsConfig

    {
    "compilerOptions": {
      "target": "es5",
      "module": "es2015",
      "moduleResolution": "node",
      "sourceMap": true,
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "lib": ["es6", "dom"],
      "noImplicitAny": true,
      "suppressImplicitAnyIndexErrors": true,
      "typeRoots": [
        "node_modules/@types/"
      ],
      "types": [
         "core-js"
       ]
    },
    

    【讨论】:

    • 还有这个问题
    • 我在帖子中都喜欢,但它仍然无法正常工作。也许问题出在 webpack 配置和路径中?
    • 问题出在哪里
    • 不知道,我只是删除项目并重新创建它
    猜你喜欢
    • 2017-03-16
    • 2017-02-01
    • 2016-05-23
    • 2016-11-05
    • 2018-08-29
    • 1970-01-01
    • 2016-01-24
    • 2017-08-14
    相关资源
    最近更新 更多