Skip to content

Commit

Permalink
fix #2388: allow consuming types without dom types (#3679)
Browse files Browse the repository at this point in the history
  • Loading branch information
remcohaszing committed Mar 7, 2024
1 parent 116f63e commit c809af0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,14 @@ test-old-ts: platform-neutral | require/old-ts/node_modules
node-unref-tests: | scripts/node_modules
node scripts/node-unref-tests.js

lib-typecheck: lib-typecheck-node lib-typecheck-deno
lib-typecheck: lib-typecheck-node lib-typecheck-node-nolib lib-typecheck-deno

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

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

lib-typecheck-deno: lib/deno/lib.deno.d.ts | lib/node_modules
cd lib && node_modules/.bin/tsc -noEmit -p tsconfig-deno.json

Expand Down
22 changes: 22 additions & 0 deletions lib/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -679,3 +679,25 @@ export let version: string
// killing it before the test ends, so you have to call this function (and
// await the returned promise) in every Deno test that uses esbuild.
export declare function stop(): Promise<void>

// Note: These declarations exist to avoid type errors when you omit "dom" from
// "lib" in your "tsconfig.json" file. TypeScript confusingly declares the
// global "WebAssembly" type in "lib.dom.d.ts" even though it has nothing to do
// with the browser DOM and is present in many non-browser JavaScript runtimes
// (e.g. node and deno). Declaring it here allows esbuild's API to be used in
// these scenarios.
//
// There's an open issue about getting this problem corrected (although these
// declarations will need to remain even if this is fixed for backward
// compatibility with older TypeScript versions):
//
// https://github.com/microsoft/TypeScript-DOM-lib-generator/issues/826
//
declare global {
namespace WebAssembly {
interface Module {
}
}
interface URL {
}
}
11 changes: 11 additions & 0 deletions lib/tsconfig-nolib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"module": "CommonJS", // Allow the "import assignment" syntax
"target": "es2017", // Allow calling APIs such as "Object.entries"
"strict": true,
"lib": [], // Omit "dom" to test what happens with the "WebAssembly" type, which is defined in "dom"
},
"include": [
"shared/types.ts",
],
}
6 changes: 1 addition & 5 deletions scripts/ts-type-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,7 @@ async function main() {
const tsc = path.join(__dirname, 'node_modules', 'typescript', 'lib', 'tsc.js')
const esbuild_d_ts = path.join(testDir, 'node_modules', 'esbuild', 'index.d.ts')
fs.mkdirSync(path.dirname(esbuild_d_ts), { recursive: true })
fs.writeFileSync(esbuild_d_ts, `
declare module 'esbuild' {
${types.replace(/export declare/g, 'export')}
}
`)
fs.writeFileSync(esbuild_d_ts, types)
let allTestsPassed = true

// Check tests without errors
Expand Down

0 comments on commit c809af0

Please sign in to comment.