Phi
Phi

Reputation: 507

TypeScript use of global type inside namespace with same type

I have a type named the same as a global type. Specifically, an Event.

I've put my Event inside a namespace, which makes referring to it outside the namespace easy, but inside the namespace I can't refer to the global (or standard) one.

namespace Dot {
    export class Event {
        // a thing happens between two parties; nothing to do with JS Event
    }
    function doStuff(e : Event) {
       // Event is presumed to be a Dot.Event instead of usual JS event
       // Unable to refer to global type?
    }
}
function doStuff2(e : Event) {
    // Use of regular Event type, cool
}
function doStuff3(e : Dot.Event) {
    // Use of Dot event type, cool
}

I suspect this is simply not possible, but can this be confirmed? Any workarounds besides renaming the Dot.Event type?

Cheers

Upvotes: 1

Views: 198

Answers (1)

Fenton
Fenton

Reputation: 251232

You could create a type to represent the global Event type, and use it within the namespace:

type GlobalEvent = Event;

namespace Dot {
    export class Event {
        // a thing happens between two parties; nothing to do with JS Event
    }
    function doStuff(e : GlobalEvent) {
       // Event is presumed to be a Dot.Event instead of usual JS event
       // Unable to refer to global type?
    }
}
function doStuff2(e : Event) {
    // Use of regular Event type, cool
}
function doStuff3(e : Dot.Event) {
    // Use of Dot event type, cool
}

But my recommendation would be to call your specialisation something else, for example DotEvent.

Upvotes: 4

Related Questions