我在下面输入了WebWorkers API。在文件的最底部是描述自我对象的一行。在lib.d.ts中使用相同的变量名称,当我添加对WebWorker.d.ts的引用时,我看到self有类型Window,而不是DedicatedWorkerGlobalScope。 我应该修复什么来覆盖这个定义?
interface WorkerNavigator extends NavigatorID, NavigatorOnLine {
}
interface WorkerUtils extends WindowTimers, WindowBase64 {
importScripts: (...urls: string[]) => void;
navigator: WorkerNavigator;
}
interface WorkerLocation {
href: string;
protocol: string;
host: string;
hostname: string;
port: string;
pathname: string;
search: string;
hash: string;
}
interface WorkerGlobalScope extends EventTarget, WorkerUtils {
self: WorkerGlobalScope;
location: WorkerLocation;
close: () => void;
onerror: Function;
onoffline: Function;
ononline: Function;
}
interface DedicatedWorkerGlobalScope extends WorkerGlobalScope {
postMessage:(message:any, ...args:any[])=>void;
onmessage: Function;
}
declare var self: DedicatedWorkerGlobalScope;
UPD: 尽管上面的代码,我必须使用以下方式。看起来自我的定义没有被覆盖
/// <reference name="WebWorkers.d.ts" />
declare var self: DedicatedWorkerGlobalScope;
答案 0 :(得分:0)
该代码有效,您可以看到它适用于最新版本的 TypeScript here
TypeScript接口是开放式的:
interface Foo{
bar:number;
}
// Continue to add members to Foo:
interface Foo{
baz: number;
}
// The followin is valid :
var foo:Foo;
foo.bar = 123;
foo.baz = 456;