gpt4 book ai didi

typescript - 覆盖 lib.d.ts 中定义的变量

转载 作者:搜寻专家 更新时间:2023-10-30 20:41:17 24 4
gpt4 key购买 nike

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

更新:尽管有上面的代码,但我必须使用以下方式。看起来 self 的定义没有被覆盖

/// <reference name="WebWorkers.d.ts" />

declare var self: DedicatedWorkerGlobalScope;

最佳答案

该代码是有效的,您可以看到它适用于最新版本的 TypeScript here

TypeScript 接口(interface)是开放式的:

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;

关于typescript - 覆盖 lib.d.ts 中定义的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18726189/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com