简体   繁体   中英

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. And I can run that code directly in Chrome's JavaScript console. But when I try to run tsc I get the following error: 在此处输入图像描述

I right clicked in VS Code. It has the expected definitions in 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. (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.

Is there a way to fix this?

I have a workaround, but it's ugly. If I cast to any it works fine.

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.

eg:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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