Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript compile error due to typings

I'm using typescript 1.7.5, typings 0.6.9 and angular 2.0.0-beta.0.

How can I get rid of the typescript compile error messages Duplicate identifier due to typings definition files?

The Duplicate identifier error occurs in the definition files of the following directories:

node_modules/angular2/typings/es6-shim/es6-shim.d.ts
node_modules/angular2/typings/jasmine/jasmine.d.ts
node_modules/angular2/typings/zone/zone.d.ts
typings/browser/ambient/es6-promise/es6-promise.d.ts
typings/browser/ambient/es6-shim/es6-shim.d.ts
typings/browser/ambient/jasmine/jasmine.d.ts
typings/browser/ambient/karma/karma.d.ts
typings/browser/ambient/zone.js/zone.js.d.ts

What's the compiler doing in node_modules/angular2 directory since I excluded it in tsconfig.json?

I also posted this question on GitHub

tsconfig.json

{
    "compilerOptions": {
        "target": "es5",
        "module": "system",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": false,
        "noImplicitAny": false
    },
    "exclude": [
        "node_modules",
        "typings/main",
        "typings/main.d.ts"
    ]
}

They are gone if I change the exclude part of tsconfig.json:

"exclude": [
    "node_modules",
    "typings"
]

But then after adding the following I get again the same Duplicate identifier compile errors:

/// <reference path="../../typings/browser.d.ts" />

typings.json

{
  "name": "example-mean-app-client",
  "dependencies": {},
  "devDependencies": {},
  "ambientDependencies": {
    "bootstrap": "github:DefinitelyTyped/DefinitelyTyped/bootstrap/bootstrap.d.ts#4de74cb527395c13ba20b438c3a7a419ad931f1c",
    "es6-promise": "github:DefinitelyTyped/DefinitelyTyped/es6-promise/es6-promise.d.ts#830e8ebd9ef137d039d5c7ede24a421f08595f83",
    "es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#4de74cb527395c13ba20b438c3a7a419ad931f1c",
    "jasmine": "github:DefinitelyTyped/DefinitelyTyped/jasmine/jasmine.d.ts#dd638012d63e069f2c99d06ef4dcc9616a943ee4",
    "karma": "github:DefinitelyTyped/DefinitelyTyped/karma/karma.d.ts#02dd2f323e1bcb8a823269f89e0909ec9e5e38b5",
    "karma-jasmine": "github:DefinitelyTyped/DefinitelyTyped/karma-jasmine/karma-jasmine.d.ts#661e01689612eeb784e931e4f5274d4ea5d588b7",
    "systemjs": "github:DefinitelyTyped/DefinitelyTyped/systemjs/systemjs.d.ts#83af898254689400de8fb6495c34119ae57ec3fe",
    "zone.js": "github:DefinitelyTyped/DefinitelyTyped/zone.js/zone.js.d.ts#9027703c0bd831319dcdf7f3169f7a468537f448"
  }
}
like image 820
Herman Fransen Avatar asked Mar 03 '16 15:03

Herman Fransen


People also ask

Does typescript no longer complain about typescript solutions anymore?

TypeScript no longer complains about your solution. :) The linked example currently goes to the default typescript playground. Can someone fix the link to load the code correctly? The author's specific issue with this seems to be solved but the question is posed about ignoring errors, and for those who end up here looking how to ignore errors:

How do I run TypeScript code in Visual Studio Code?

Visual Studio Code includes TypeScript language support but does not include the TypeScript compiler, tsc. You will need to install the TypeScript compiler either globally or in your workspace to transpile TypeScript source code to JavaScript ( tsc HelloWorld.ts ). The easiest way to install TypeScript is through npm, the Node.js Package Manager.

How to transpile TypeScript into JavaScript?

Transpile TypeScript into JavaScript #. Step 1: Create a simple TS file #. Open VS Code on an empty folder and create a helloworld.ts file, place the following code in that file... Step 2: Run the TypeScript build #. Step 3: Make the TypeScript Build the default #. Step 4: Reviewing build issues #. ...

Is it possible to configure the TypeScript compiler to handle modules?

With the common TypeScript modules problems highlighted, and solutions provided in this post, I hope that it will become a bit easier to configure the TypeScript compiler to handle modules in your TypeScript projects. Hopefully you’ve found this post informative and helpful.


3 Answers

For me, choosing either 'browser' or 'main' (depending on your application: front end or back end) and excluding the other one in tsconfig.json worked:

  "exclude": [
    "node_modules",
    "wwwroot",
    "typings/main",
    "typings/main.d.ts"
  ]
like image 72
fikkatra Avatar answered Oct 18 '22 22:10

fikkatra


As basarat alludes to, you can either change:

"moduleResolution": "node",

to

"moduleResolution": "classic",

Or you can simply delete all of the duplicate typings from the typings folder. What's happening is that it's automatically importing all of the typings from the the node_modules folder of every import you do in your code. It's also importing the typings that are dependencies of the browser.d.ts file.

like image 3
rgvassar Avatar answered Oct 18 '22 23:10

rgvassar


What's the compiler doing in node_modules/angular2 directory since I excluded it in tsconfig.json

Its looking at npm modules becuase of "moduleResolution": "node", but only the files that are imported (without the exclude it would look at all the files).

like image 3
basarat Avatar answered Oct 18 '22 23:10

basarat