TypeScript 在具有相同类型的命名空间内使用全局类型
TypeScript use of global type inside namespace with same type
我有一个与全局类型同名的类型。具体来说,一个事件。
我已将我的事件放在命名空间中,这使得在命名空间外引用它变得容易,但在命名空间内我无法引用全局(或标准)事件。
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 类型之外还有其他解决方法吗?
干杯
您可以创建一个类型来表示全局 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
。
我有一个与全局类型同名的类型。具体来说,一个事件。
我已将我的事件放在命名空间中,这使得在命名空间外引用它变得容易,但在命名空间内我无法引用全局(或标准)事件。
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 类型之外还有其他解决方法吗?
干杯
您可以创建一个类型来表示全局 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
。