2017-10-04 149 views
0

我有一個類型與全局類型相同。具體來說,一個事件。TypeScript使用相同類型的全局類型名稱空間

我把我的事件放在一個命名空間內,這使得它在命名空間外部容易引用,但是在命名空間內我不能引用全局(或標準)命名空間。

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 
} 

我懷疑這根本不可能,但是可以證實這一點嗎?除重命名Dot.Event類型之外的任何解決方法?

乾杯

+1

相關:https://github.com/Microsoft/TypeScript/issues/983 – xmojmr

回答

3

可能創建一個類型來表示全球Event類型和命名空間內使用它:

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 
} 

但我的建議是別人叫你的專業化的東西,對於示例DotEvent

+0

這就是我一直在尋找的。 Dot的大部分代碼都使用DotEvent,它只是Dot中需要標準事件的事件監聽器,所以我將使用GlobalEvent方法。乾杯 – Phi

相關問題