6

我在下面输入了 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;
4

1 回答 1

1

该代码是有效的,您可以看到它适用于最新版本的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; 
于 2013-09-10T21:41:56.053 回答