Skip to content

Commit c809af0

Browse files
authored
fix #2388: allow consuming types without dom types (#3679)
1 parent 116f63e commit c809af0

4 files changed

Lines changed: 38 additions & 6 deletions

File tree

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,14 @@ test-old-ts: platform-neutral | require/old-ts/node_modules
107107
node-unref-tests: | scripts/node_modules
108108
node scripts/node-unref-tests.js
109109

110-
lib-typecheck: lib-typecheck-node lib-typecheck-deno
110+
lib-typecheck: lib-typecheck-node lib-typecheck-node-nolib lib-typecheck-deno
111111

112112
lib-typecheck-node: | lib/node_modules
113113
cd lib && node_modules/.bin/tsc -noEmit -p tsconfig.json
114114

115+
lib-typecheck-node-nolib: | lib/node_modules
116+
cd lib && node_modules/.bin/tsc -noEmit -p tsconfig-nolib.json
117+
115118
lib-typecheck-deno: lib/deno/lib.deno.d.ts | lib/node_modules
116119
cd lib && node_modules/.bin/tsc -noEmit -p tsconfig-deno.json
117120

lib/shared/types.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,3 +679,25 @@ export let version: string
679679
// killing it before the test ends, so you have to call this function (and
680680
// await the returned promise) in every Deno test that uses esbuild.
681681
export declare function stop(): Promise<void>
682+
683+
// Note: These declarations exist to avoid type errors when you omit "dom" from
684+
// "lib" in your "tsconfig.json" file. TypeScript confusingly declares the
685+
// global "WebAssembly" type in "lib.dom.d.ts" even though it has nothing to do
686+
// with the browser DOM and is present in many non-browser JavaScript runtimes
687+
// (e.g. node and deno). Declaring it here allows esbuild's API to be used in
688+
// these scenarios.
689+
//
690+
// There's an open issue about getting this problem corrected (although these
691+
// declarations will need to remain even if this is fixed for backward
692+
// compatibility with older TypeScript versions):
693+
//
694+
// https://github.com/microsoft/TypeScript-DOM-lib-generator/issues/826
695+
//
696+
declare global {
697+
namespace WebAssembly {
698+
interface Module {
699+
}
700+
}
701+
interface URL {
702+
}
703+
}

lib/tsconfig-nolib.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
"module": "CommonJS", // Allow the "import assignment" syntax
4+
"target": "es2017", // Allow calling APIs such as "Object.entries"
5+
"strict": true,
6+
"lib": [], // Omit "dom" to test what happens with the "WebAssembly" type, which is defined in "dom"
7+
},
8+
"include": [
9+
"shared/types.ts",
10+
],
11+
}

scripts/ts-type-tests.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,11 +379,7 @@ async function main() {
379379
const tsc = path.join(__dirname, 'node_modules', 'typescript', 'lib', 'tsc.js')
380380
const esbuild_d_ts = path.join(testDir, 'node_modules', 'esbuild', 'index.d.ts')
381381
fs.mkdirSync(path.dirname(esbuild_d_ts), { recursive: true })
382-
fs.writeFileSync(esbuild_d_ts, `
383-
declare module 'esbuild' {
384-
${types.replace(/export declare/g, 'export')}
385-
}
386-
`)
382+
fs.writeFileSync(esbuild_d_ts, types)
387383
let allTestsPassed = true
388384

389385
// Check tests without errors

0 commit comments

Comments
 (0)