From 3b34f2b36f8d1732adb5309d93808f54102230ac Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Thu, 5 Dec 2024 11:42:48 +0100 Subject: [PATCH 1/2] Make tsc check JS files with @ts-check annotations --- index.js | 2 +- index.mjs | 2 +- node.js | 3 ++- node.mjs | 5 +++-- tsconfig.json | 3 ++- 5 files changed, 9 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 142164de6..d22c3ec7c 100644 --- a/index.js +++ b/index.js @@ -63,7 +63,7 @@ async function loadModule() { mod = await WebAssembly.compile(bytes); } - /** @type {{exports: typeof import("./pkg/matrix_sdk_crypto_wasm_bg.wasm.d")}} */ + /** @type {{exports: typeof import("./pkg/matrix_sdk_crypto_wasm_bg.wasm.d.ts")}} */ // @ts-expect-error: Typescript doesn't know what the instance exports exactly const instance = new WebAssembly.Instance(mod, { // @ts-expect-error: The bindings don't exactly match the 'ExportValue' type diff --git a/index.mjs b/index.mjs index b285cc1e7..f55cc01e7 100644 --- a/index.mjs +++ b/index.mjs @@ -63,7 +63,7 @@ async function loadModule() { mod = await WebAssembly.compile(bytes); } - /** @type {{exports: typeof import("./pkg/matrix_sdk_crypto_wasm_bg.wasm.d")}} */ + /** @type {{exports: typeof import("./pkg/matrix_sdk_crypto_wasm_bg.wasm.d.ts")}} */ // @ts-expect-error: Typescript doesn't know what the instance exports exactly const instance = new WebAssembly.Instance(mod, { // @ts-expect-error: The bindings don't exactly match the 'ExportValue' type diff --git a/node.js b/node.js index b94a33eb8..4c71fc4fe 100644 --- a/node.js +++ b/node.js @@ -35,6 +35,7 @@ bindings.__wbg_set_wasm( { get(_target, prop) { const mod = loadModuleSync(); + // @ts-expect-error: This results to an `any` type, which is fine return initInstance(mod)[prop]; }, }, @@ -85,7 +86,7 @@ async function loadModule() { function initInstance(mod) { if (initialised) throw new Error("initInstance called twice"); - /** @type {{exports: typeof import("./pkg/matrix_sdk_crypto_wasm_bg.wasm.d")}} */ + /** @type {{exports: typeof import("./pkg/matrix_sdk_crypto_wasm_bg.wasm.d.ts")}} */ // @ts-expect-error: Typescript doesn't know what the instance exports exactly const instance = new WebAssembly.Instance(mod, { // @ts-expect-error: The bindings don't exactly match the 'ExportValue' type diff --git a/node.mjs b/node.mjs index 1dab77875..96d56136b 100644 --- a/node.mjs +++ b/node.mjs @@ -35,6 +35,7 @@ bindings.__wbg_set_wasm( { get(_target, prop) { const mod = loadModuleSync(); + // @ts-expect-error: This results to an `any` type, which is fine return initInstance(mod)[prop]; }, }, @@ -80,12 +81,12 @@ async function loadModule() { * Initializes the WASM module and returns the exports from the WASM module. * * @param {WebAssembly.Module} mod - * @returns {typeof import("./pkg/matrix_sdk_crypto_wasm_bg.wasm.d")} + * @returns {typeof import("./pkg/matrix_sdk_crypto_wasm_bg.wasm.d.ts")} */ function initInstance(mod) { if (initialised) throw new Error("initInstance called twice"); - /** @type {{exports: typeof import("./pkg/matrix_sdk_crypto_wasm_bg.wasm.d")}} */ + /** @type {{exports: typeof import("./pkg/matrix_sdk_crypto_wasm_bg.wasm.d.ts")}} */ // @ts-expect-error: Typescript doesn't know what the instance exports exactly const instance = new WebAssembly.Instance(mod, { // @ts-expect-error: The bindings don't exactly match the 'ExportValue' type diff --git a/tsconfig.json b/tsconfig.json index 8f51ae226..3c64f4579 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,8 @@ { "extends": "@tsconfig/node18/tsconfig.json", "compilerOptions": { - "lib": ["DOM"] + "lib": ["DOM"], + "allowJs": true }, "typedocOptions": { "entryPoints": ["index.d.ts"], From a9cd274406a4086c2d80556f144eb9c42e5d3ede Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Mon, 9 Dec 2024 17:40:14 +0100 Subject: [PATCH 2/2] Fix the precedence of exports, so TypeScript loads the right types In some contexts, it would try to use the JS file and type it instead of the proper TS definitions --- package.json | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index d3bbf70c7..09aecfed9 100644 --- a/package.json +++ b/package.json @@ -20,13 +20,16 @@ ], "exports": { ".": { - "node": { - "require": "./node.js", - "import": "./node.mjs" + "require": { + "types": "./index.d.ts", + "node": "./node.js", + "default": "./index.js" }, - "require": "./index.js", - "import": "./index.mjs", - "types": "./index.d.ts" + "import": { + "types": "./index.d.ts", + "node": "./node.mjs", + "default": "./index.mjs" + } } }, "files": [