简体   繁体   English

在 TypeScript 中使用 MemoryDescriptor(用于新的 WebAssembly.Memory())

[英]Using MemoryDescriptor (for new WebAssembly.Memory()) in TypeScript

I'm trying to use the following code:我正在尝试使用以下代码:

const test = new WebAssembly.Memory({initial : 2, shared : true, maximum : 3});

IntelliSense in VS Code says everything is good. VS Code 中的 IntelliSense 表示一切都很好。 And I can run that code directly in Chrome's JavaScript console.我可以直接在 Chrome 的 JavaScript 控制台中运行该代码。 But when I try to run tsc I get the following error:但是当我尝试运行tsc时,出现以下错误: 在此处输入图像描述

I right clicked in VS Code.我右键单击 VS Code。 It has the expected definitions in lib.dom.ts.d .它在lib.dom.ts.d中有预期的定义。

var Memory: {
    prototype: Memory;
    new(descriptor: MemoryDescriptor): Memory;
};
interface MemoryDescriptor {
    initial: number;
    maximum?: number;
    shared?: boolean;
}

However, I see 7 copies of lib.dom.ts.d on my computer.但是,我在我的计算机上看到了 7 个lib.dom.ts.d副本。 (There were 6 before I upgraded to the latest version of typescript.) Based on the file sizes there are at least three different versions of this file. (在我升级到最新版本的 typescript 之前有 6 个。)根据文件大小,这个文件至少有三个不同的版本。

Is there a way to fix this?有没有办法来解决这个问题?

I have a workaround, but it's ugly.我有一个解决方法,但它很难看。 If I cast to any it works fine.如果我投射到any它工作正常。

const test = new WebAssembly.Memory({initial : 2, shared : true, maximum : 3} as any);

the shared property was missing in TypeScript-DOM-lib-generator , but last month (Feb/2021) someone added it to the MemoryDescriptor interface ( pull request ), you can use the latest version or re-write it until the stable version is released. TypeScript-DOM-lib-generator中缺少shared属性,但上个月(2021 年 2 月)有人将其添加到MemoryDescriptor接口( 拉取请求)中,您可以使用最新版本或重新编写它直到稳定版本释放。

eg:例如:

interface SharedMemoryDescriptor {
    initial: number;
    maximum?: number;
    shared?: boolean;
}

const test = new WebAssembly.Memory({initial : 2, shared : true, maximum : 3} as SharedMemoryDescriptor);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM