angular 2 multiple times error TS2300: Duplicate i

2019-07-07 18:01发布

问题:

system configuraions

  • ubuntu 14.04
  • node -v => v5.6.0
  • npm -v => 3.7.1
  • typings latest version (don't know how to get version detail)

while working with angular2 first time on my folder sample/server structure is below

|-- server.js
|-- server.ts
|-- tsconfig.json
|-- typings
|   |-- browser
|   |   `-- ambient
|   |       |-- express
|   |       |   `-- express.d.ts
|   |       |-- mime
|   |       |   `-- mime.d.ts
|   |       |-- node
|   |       |   `-- node.d.ts
|   |       `-- serve-static
|   |           `-- serve-static.d.ts
|   |-- browser.d.ts
|   |-- main
|   |   `-- ambient
|   |       |-- express
|   |       |   `-- express.d.ts
|   |       |-- mime
|   |       |   `-- mime.d.ts
|   |       |-- node
|   |       |   `-- node.d.ts
|   |       `-- serve-static
|   |           `-- serve-static.d.ts
|   `-- main.d.ts
`-- typings.json

tsconfig.json

{
  "compilerOptions": {
    "target": "ES5",
    "module": "commonjs",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
   "files": [
    "./server/typings/main.d.ts",
    "./server/server.ts"
  ],
  "exclude": [
        "node_modules", 
        // even used ../node_modules as the folder is located above this folder
        "typings/browser.d.ts",
        "typings/browser"
  ]
}

typings.json

{
  "ambientDependencies": {
    "express": "github:DefinitelyTyped/DefinitelyTyped/express/express.d.ts#d1f6bde13f2209be42e86c3686761e8bfcbb50a5",
    "mime": "github:DefinitelyTyped/DefinitelyTyped/mime/mime.d.ts#d1f6bde13f2209be42e86c3686761e8bfcbb50a5",
    "node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#aee0039a2d6686ec78352125010ebb38a7a7d743",
    "serve-static": "github:DefinitelyTyped/DefinitelyTyped/serve-static/serve-static.d.ts#0fa4e9e61385646ea6a4cba2aef357353d2ce77f"
  }
}

gulpgile.js

var path = require('path');
var gulp = require('gulp');
var gutil = require('gulp-util');
var ts = require('gulp-typescript');

gulp.task('log', function() {
  gutil.log('== My Log Task ==')
});

gulp.task('buildServer', function () {
    var tsProject = ts.createProject('./server/tsconfig.json');
    return gulp.src('./server/**/*.ts')
        .pipe(ts(tsProject))
        .js
        .pipe(gulp.dest('./server'))
});

main.d.ts and browser.d.ts

/// <reference path="main/ambient/express/express.d.ts" />
/// <reference path="main/ambient/mime/mime.d.ts" />
/// <reference path="main/ambient/node/node.d.ts" />
/// <reference path="main/ambient/serve-static/serve-static.d.ts" />

when run

usernam@hostname:~/sample$ gulp buildServer

it gives 655 errors similar to below , referencing every line of node.d.ts

server/typings/browser/ambient/node/node.d.ts(754,9): error TS2300: Duplicate identifier 'internal'.

...
....

server/typings/main/ambient/node/node.d.ts(1943,18): error TS2300: Duplicate identifier 'Domain'.

...

.....

server/typings/main/ambient/serve-static/serve-static.d.ts(85,5): error TS2300: Duplicate identifier 'export='.

and finally it's stops

[22:38:56] TypeScript: 655 semantic errors
[22:38:56] TypeScript: emit succeeded (with errors)

UPDATE

I notice it gives error first for main/ folder than for browser/ folder

my guess is that issue is with gulpfile.js ; make below change in gulpfile.js

return gulp.src('./server/**/*.ts') to return gulp.src('./server/main/*.ts')

then gulp buildServer runs but finished instantly.

  • What does this .js do?
  • Can gulp.dest and gulp.src pointed on same folder?

Please suggest what is the work around to fix this.I have seen over the web but couldn't find anything working for now.

回答1:

Instead of including all *.ts files including the definition files try just including all *.ts files excluding all *.d.ts files except for main.d.ts.

gulp.src(['server/**/*.ts', '!server/**/*.d.ts', 'server/typings/main.d.ts'])

or (not 100% on how the ordering works in glob off the top of my head)

gulp.src(['server/**/*.ts', 'server/typings/main.d.ts', '!server/**/*.d.ts'])

Update: I ended up using merge2 to combine two gulp.src streams.

var gulp = require('gulp');
var merge = require('merge2');
var typescript = require('gulp-typescript');

gulp.task('build', function() {
    var tsFiles = gulp.src('server/**/*.ts', '!server/typings/**');

    return merge(tsFiles, gulp.src('server/typings/main.d.ts'))
        .pipe(typescript(...));
});