打字稿加载程序会抛出多个重复的标识符。'编译时出错

时间:2017-02-23 20:02:54

标签: typescript webpack visual-studio-code

我今天将一个项目从我的工作站移到我的家用电脑上,现在我不能再编译它了。

每当我正在运行webpack'时,我都会收到以下错误:

TS2300: Duplicate identifier 'Request'.

ERROR in [at-loader] C:\Users\Rat King Cole\AppData\Roaming\npm\node_modules\typescript\lib\lib.dom.d.ts:9360:13
    TS2300: Duplicate identifier 'Request'.

ERROR in [at-loader] C:\Users\Rat King Cole\AppData\Roaming\npm\node_modules\typescript\lib\lib.dom.d.ts:9365:11
    TS2300: Duplicate identifier 'Response'.

ERROR in [at-loader] C:\Users\Rat King Cole\AppData\Roaming\npm\node_modules\typescript\lib\lib.dom.d.ts:9376:13
    TS2300: Duplicate identifier 'Response'.

ERROR in [at-loader] C:\Users\Rat King Cole\AppData\Roaming\npm\node_modules\typescript\lib\lib.dom.d.ts:14893:18
    TS2451: Cannot redeclare block-scoped variable 'fetch'.

ERROR in [at-loader] C:\Users\Rat King Cole\AppData\Roaming\npm\node_modules\typescript\lib\lib.dom.d.ts:14898:6
    TS2300: Duplicate identifier 'BodyInit'.

ERROR in [at-loader] C:\Users\Rat King Cole\AppData\Roaming\npm\node_modules\typescript\lib\lib.dom.d.ts:14919:6
    TS2300: Duplicate identifier 'HeadersInit'.

ERROR in [at-loader] C:\Users\Rat King Cole\AppData\Roaming\npm\node_modules\typescript\lib\lib.dom.d.ts:14929:6
    TS2300: Duplicate identifier 'RequestInfo'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:11:13
    TS2451: Cannot redeclare block-scoped variable 'fetch'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:13:14
    TS2300: Duplicate identifier 'HeadersInit'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:14:15
    TS2300: Duplicate identifier 'Headers'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:31:14
    TS2300: Duplicate identifier 'BodyInit'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:43:14
    TS2300: Duplicate identifier 'RequestInfo'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:44:15
    TS2300: Duplicate identifier 'Request'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:64:11
    TS2300: Duplicate identifier 'Request'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:70:5
    TS2403: Subsequent variable declarations must have the same type.  Variable 'referrerPolicy' must be of type 'string', but here has type 'ReferrerPolicy'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:71:5
    TS2403: Subsequent variable declarations must have the same type.  Variable 'mode' must be of type 'string', but here has type 'RequestMode'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:72:5
    TS2403: Subsequent variable declarations must have the same type.  Variable 'credentials' must be of type 'string', but here has type 'RequestCredentials'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:73:5
    TS2403: Subsequent variable declarations must have the same type.  Variable 'cache' must be of type 'string', but here has type 'RequestCache'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:74:5
    TS2403: Subsequent variable declarations must have the same type.  Variable 'redirect' must be of type 'string', but here has type 'RequestRedirect'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:76:5
    TS2403: Subsequent variable declarations must have the same type.  Variable 'window' must be of type 'any', but here has type 'null'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:88:15
    TS2300: Duplicate identifier 'Response'.

ERROR in [at-loader] node_modules\@types\whatwg-fetch\index.d.ts:107:11
    TS2300: Duplicate identifier 'Response'.

ERROR in [at-loader] node_modules\@types\whatwg-streams\index.d.ts:32:15
    TS2300: Duplicate identifier 'ReadableStream'.

我已经删除并重新安装了所有node_modules(和类型),尝试在本地和全局安装Typescript,并从ts-loader切换到awesome-typescript-loader(至少解决了我的其他一些错误)。

tsconfig.json

{
    "compilerOptions": {
        "baseUrl": "app",
        "moduleResolution": "node",
        "module": "commonjs",
        "target": "es6",
        "jsx": "preserve",
        "noImplicitAny": false,
        "sourceMap": true,
        "lib": [
            "es6",
            "dom"
        ],
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true
    },
    "exclude": [
        "node_modules"
    ]
}

webpack.config.js

var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
const { CheckerPlugin } = require('awesome-typescript-loader')
var TsConfigPathsPlugin = require('awesome-typescript-loader').TsConfigPathsPlugin;

module.exports = {
    entry: {
        'vendor': [
            'react',
            'react-dom',
            'react-router',
            'react-bootstrap',
            'react-router-bootstrap',
            'react-datetime'
        ],
        'client': [
            'webpack-dev-server/client?http://0.0.0.0:8080', // WebpackDevServer host and port
            'webpack/hot/only-dev-server', // "only" prevents reload on syntax errors
            './app/boot-client.tsx'
        ],
    },
    output: {
        filename: '[name].bundle.js',
        path: './app/',
    },
    resolve: {
        plugins: [
            new TsConfigPathsPlugin('./tsconfig.json')
        ],
        extensions: ['.js', '.jsx', '.ts', '.tsx', '.scss']
    },
    module: {
        loaders: [
            {
                test: /\.tsx?$/,
                include: /app/,
                loaders: 'babel-loader',
                query: {
                    presets: ['es2015']
                }
            },
            {
                test: /\.tsx?$/,
                include: /app/,
                loaders: 'awesome-typescript-loader'
            },
            {
                test: /\.scss$/,
                loaders: ['style-loader', 'css-loader', 'sass-loader']
            }
        ]
    },
    plugins: [
        new webpack.optimize.CommonsChunkPlugin('vendor'),
        new HtmlWebpackPlugin({
            inject: 'body',
            template: 'app/index_template.html',
            filename: 'index.html'
        }),
        new webpack.HotModuleReplacementPlugin(),
        new CheckerPlugin()
    ],
    devServer: {
        contentBase: "./app",
        port: 8080
    },
};

不幸的是,我在这方面不是很有经验,所以我在这里缺少什么?

1 个答案:

答案 0 :(得分:33)

  

[at-loader]中的错误node_modules \ @types \ whatwg-fetch \ index.d.ts:11:13

TypeScript最新版带有开箱即用的提取定义。因此,请卸载whatwg-fetch个定义:

npm uninstall @types/whatwg-fetch