Glen Little
Glen Little

Reputation: 7128

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?

Upvotes: 2

Views: 712

Answers (2)

RKS_Code
RKS_Code

Reputation: 530

Navigator is defined in lib.d.ts. systemLanguage is no longer a property of Navigator interface. Check here https://github.com/Microsoft/TypeScript/blob/master/lib/lib.d.ts

Looks like commit made on Apr 11, 2016 removes the systemLanguage.

Upvotes: 1

GPicazo
GPicazo

Reputation: 6676

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;
}

Upvotes: 1

Related Questions