【问题标题】:TypeScript error on compilation with webpack使用 webpack 编译时出现 TypeScript 错误
【发布时间】:2016-12-08 05:19:22
【问题描述】:

我在 typescript 编译时遇到错误,我不知道它来自哪里 - 我尝试了许多不同的 tsconfig.json 设置组合,但我总是从 webpack-env 中得到相同的两个错误。

(gec) ➜  optim ./node_modules/.bin/webpack --config webpack.config.js
ts-loader: Using typescript@1.8.10 and /home/bischoff_s/Code/optim/tsconfig.json
Hash: 0e608742cc4dff532c9a
Version: webpack 1.13.1
Time: 3182ms
                            Asset      Size      Chunks             Chunk Names
     main-0e608742cc4dff532c9a.ts   5.09 MB  0, 1, 2, 3  [emitted]  main
polyfills-0e608742cc4dff532c9a.ts  25 bytes     1, 2, 3  [emitted]  polyfills
      app-0e608742cc4dff532c9a.ts  25 bytes     2, 1, 3  [emitted]  app
   vendor-0e608742cc4dff532c9a.ts   3.63 kB     3, 1, 2  [emitted]  vendor
    + 329 hidden modules

ERROR in /home/guy/Code/optim/typings/globals/webpack-env/index.d.ts
(176,13): error TS2403: Subsequent variable declarations must have the same type.  Variable 'require' must be of type 'NodeRequire', but here has type 'RequireFunction'.

ERROR in /home/guyCode/optim/typings/globals/webpack-env/index.d.ts
(225,13): error TS2403: Subsequent variable declarations must have the same type.  Variable 'module' must be of type 'NodeModule', but here has type 'Module'.

这是我的 tsconfig.json

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "sourceMap": true,
        "noEmitHelpers": true
    }

}

【问题讨论】:

    标签: javascript typescript webpack typescript1.8


    【解决方案1】:

    你为 webpack-env 和 node 安装了类型——这两个模块都有 requiremodule 的环境声明。 Tsc 抱怨 require/module 不能用不同的类型重新声明(webpack-env/index.d.ts 的第 176 和 225 行)。你真的在使用 webpack-env 类型吗?尝试删除 webpack-env/index.d.ts 看看。

    【讨论】:

    • 我删除它并得到错误 (4,1): error TS6053: File '/webpack-env/index.d.ts' not found。什么是 webpack-env?我的 webpack 配置或节点 package.json 中的任何地方都没有引用它
    • 查看你的 typings/index.d.ts 中的 reference 指令——它将引用(现在)缺少的 webpack-env 类型。确保它也从您的 typings.json 文件中消失,否则打字安装将重新引入它。
    • 成功了,谢谢!
    • 如何移除 webpack-env?我没有在 package.json 和 typings.json 中引用它。
    • 这是很久以前的事了,typings.json 早就被弃用了。我会一起删除 typings.son,并使用“@types”前缀直接从 package.json 中引用类型。您是否在使用旧代码库?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-21
    • 2016-06-21
    • 2021-05-21
    • 2018-08-23
    • 2019-08-01
    • 1970-01-01
    • 2020-09-19
    相关资源
    最近更新 更多