Glen Little Glen Little - 10 days ago 11
TypeScript Question

TypeScript - duplicate identifier

In my TypeScript 2.0 code (in Visual Studio 2015), I have:

var lang = navigator.language
? navigator.language
: navigator.systemLanguage;


With that, I get this error:

Build:Property 'systemLanguage' does not exist on type 'Navigator'.


However, if I add this to one of my d.ts files:

interface Navigator {
systemLanguage: string;
}


Then I get:

TS2300 Duplicate identifier 'systemLanguage'.


How do I break this TypeScript deadlock?

Answer

From your question, I am deducing that navigator is a variable of type Navigator. From the first error you posted, I would have guessed that the issue is that either (1) that Navigator interface does not have a property called systemLanguage or (2) that you are not specifying that navigator is of type Navigator in your code file. From the second error you posted, I am fairly certain that your problem is due to issue (2).

Since Typescript compiles all .ts files, when you add the Navigator interface with the systemLanguage property, it's finding that there is already another interface with the same name and same property, thus the second error.

I think what you need to do is (1) find where Navigation is defined, (2) import that definition into the file that contains the code that you reference first and (3) type navigator as being of type Navigator.

EDIT: Here is an example:

// your-code-file.ts
import Navigator from './path-to-code-file-with-navigator/navigator.ts'

...

let navigator:Navigator = ...;

...

var lang = navigator.language 
         ? navigator.language 
         : navigator.systemLanguage;


// navigator.ts
interface Navigator {
    language: string;
    systemLanguage: string;
}