diff --git a/types/node/globals.d.ts b/types/node/globals.d.ts index e43e57b35628df..d7c6852913c412 100644 --- a/types/node/globals.d.ts +++ b/types/node/globals.d.ts @@ -2,7 +2,7 @@ export {}; // Make this a module // #region Fetch and friends // Conditional type aliases, used at the end of this file. -// Will either be empty if lib-dom is included, or the undici version otherwise. +// Will either be empty if lib.dom (or lib.webworker) is included, or the undici version otherwise. type _Request = typeof globalThis extends { onmessage: any } ? {} : import("undici-types").Request; type _Response = typeof globalThis extends { onmessage: any } ? {} : import("undici-types").Response; type _FormData = typeof globalThis extends { onmessage: any } ? {} : import("undici-types").FormData; @@ -17,6 +17,49 @@ type _WebSocket = typeof globalThis extends { onmessage: any } ? {} : import("un type _EventSource = typeof globalThis extends { onmessage: any } ? {} : import("undici-types").EventSource; // #endregion Fetch and friends +// Conditional type definitions for webstorage interface, which conflicts with lib.dom otherwise. +type _Storage = typeof globalThis extends { onabort: any } ? {} : { + /** + * Returns the number of key/value pairs. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Storage/length) + */ + readonly length: number; + /** + * Removes all key/value pairs, if there are any. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Storage/clear) + */ + clear(): void; + /** + * Returns the current value associated with the given key, or null if the given key does not exist. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Storage/getItem) + */ + getItem(key: string): string | null; + /** + * Returns the name of the nth key, or null if n is greater than or equal to the number of key/value pairs. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Storage/key) + */ + key(index: number): string | null; + /** + * Removes the key/value pair with the given key, if a key/value pair with the given key exists. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Storage/removeItem) + */ + removeItem(key: string): void; + /** + * Sets the value of the pair identified by key to value, creating a new key/value pair if none existed for key previously. + * + * Throws a "QuotaExceededError" DOMException exception if the new value couldn't be set. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Storage/setItem) + */ + setItem(key: string, value: string): void; + [key: string]: any; +}; + declare global { // Declare "static" methods in Error interface ErrorConstructor { @@ -109,54 +152,10 @@ declare global { * * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Storage) */ - interface Storage { - /** - * Returns the number of key/value pairs. - * - * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Storage/length) - */ - readonly length: number; - /** - * Removes all key/value pairs, if there are any. - * - * Dispatches a storage event on Window objects holding an equivalent Storage object. - * - * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Storage/clear) - */ - clear(): void; - /** - * Returns the current value associated with the given key, or null if the given key does not exist. - * - * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Storage/getItem) - */ - getItem(key: string): string | null; - /** - * Returns the name of the nth key, or null if n is greater than or equal to the number of key/value pairs. - * - * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Storage/key) - */ - key(index: number): string | null; - /** - * Removes the key/value pair with the given key, if a key/value pair with the given key exists. - * - * Dispatches a storage event on Window objects holding an equivalent Storage object. - * - * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Storage/removeItem) - */ - removeItem(key: string): void; - /** - * Sets the value of the pair identified by key to value, creating a new key/value pair if none existed for key previously. - * - * Throws a "QuotaExceededError" DOMException exception if the new value couldn't be set. (Setting could fail if, e.g., the user has disabled storage for the site, or if the quota has been exceeded.) - * - * Dispatches a storage event on Window objects holding an equivalent Storage object. - * - * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Storage/setItem) - */ - setItem(key: string, value: string): void; - } + interface Storage extends _Storage {} - var Storage: typeof globalThis extends { onmessage: any; Storage: infer T } ? T + // Conditional on `onabort` rather than `onmessage`, in order to exclude lib.webworker + var Storage: typeof globalThis extends { onabort: any; Storage: infer T } ? T : { prototype: Storage; new(): Storage; diff --git a/types/node/node-tests-webworker.ts b/types/node/node-tests-webworker.ts new file mode 100644 index 00000000000000..130f2e85d3f535 --- /dev/null +++ b/types/node/node-tests-webworker.ts @@ -0,0 +1,5 @@ +// Currently, all of the DOM-specific tests target APIs that are also available in WebWorkers. +// If this changes, and it's necessary to create a WebWorker-specific test script, then the import should be updated here. +import "./test/events-dom"; +import "./test/globals-dom"; +import "./test/perf_hooks-dom"; diff --git a/types/node/package.json b/types/node/package.json index f5400082146221..aa79c1ca191385 100644 --- a/types/node/package.json +++ b/types/node/package.json @@ -7,7 +7,7 @@ "projects": [ "https://nodejs.org/" ], - "tsconfigs": ["tsconfig.dom.json", "tsconfig.non-dom.json"], + "tsconfigs": ["tsconfig.dom.json", "tsconfig.non-dom.json", "tsconfig.webworker.json"], "dependencies": { "undici-types": "~6.19.2" }, diff --git a/types/node/test/globals.ts b/types/node/test/globals.ts index 1a7ab90662d9c8..e0060528f3f4f9 100644 --- a/types/node/test/globals.ts +++ b/types/node/test/globals.ts @@ -51,3 +51,13 @@ declare var RANDOM_GLOBAL_VARIABLE: true; x.reason; // $ExpectType any x.throwIfAborted(); // $ExpectType void } + +{ + const s = new Storage(); + s.setItem("foo", "bar"); + s.getItem("foo"); // $ExpectType string | null + s["foo"] = "baz"; + s["foo"]; // $ExpectType any + delete s["foo"]; + s.clear(); +} diff --git a/types/node/tsconfig.json b/types/node/tsconfig.json index 4717928cf79b4c..df64dfe038156f 100644 --- a/types/node/tsconfig.json +++ b/types/node/tsconfig.json @@ -3,7 +3,8 @@ "index.d.ts", "node-tests.ts", "node-tests-dom.ts", - "node-tests-non-dom.ts" + "node-tests-non-dom.ts", + "node-tests-webworker.ts" ], "compilerOptions": { "module": "commonjs", diff --git a/types/node/tsconfig.webworker.json b/types/node/tsconfig.webworker.json new file mode 100644 index 00000000000000..5485e4cff08757 --- /dev/null +++ b/types/node/tsconfig.webworker.json @@ -0,0 +1,22 @@ +{ + "files": [ + "index.d.ts", + "node-tests.ts", + "node-tests-webworker.ts" + ], + "compilerOptions": { + "module": "commonjs", + "target": "esnext", + "lib": [ + "es6", + "webworker" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + } +}