我在下面输入了 WebWorkers API。在文件的最底部是描述 self 对象的行。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: 尽管有上面的代码,但我必须使用以下方式。看起来 self 的定义没有被覆盖
/// <reference name="WebWorkers.d.ts" />
declare var self: DedicatedWorkerGlobalScope;