Roly
Roly

Reputation: 2237

Hiding an import from TypeScript "prelude"

If I try to define a class with the same name as a type which is imported automatically by TypeScript, such as

class Map {
}

then I get the following error

error TS2000: Duplicate identifier 'Map'.

What I would like to do is to be able to rename, or avoid importing entirely, the TypeScript library class Map, so that I can define my own with the same name.

Putting my Map in a module (as per one of the answers below) helps, but I still can't refer to it by unqualified name (i.e. by importing), although this time there is no complaint about duplicate names; the import simply doesn't do anything:

Suppose A.ts contains:

module A {
   export class Map {
   }
}

and B.ts contains:

/// <reference path='A.ts'/>

import Map = A.Map

function test (m: Map) {
}

In order to make this compile I need to replace m: Map by m: A.Map. Otherwise the compiler complains that I'm missing some generic arguments, because it assumes I mean the Map type from the TypeScript "prelude".

I feel like I should be able to define a "local" name (via an explicit declaration, or via an import) which hides any equivalently-named definition in the prelude; or, I should be able to manually disable the importing of particular types from the prelude (as I can in Haskell).

Upvotes: 0

Views: 676

Answers (1)

Alex Dresko
Alex Dresko

Reputation: 5213

Put your class inside a module..

module SomeNamespace {
    class Map {
    }
}

This will make your class unique from the default Map class.

Upvotes: 2

Related Questions