Skip to content

Commit

Permalink
🤖 Merge PR DefinitelyTyped#70328 [node] Fix Storage declaration in We…
Browse files Browse the repository at this point in the history
…bWorkers, run DT tests with lib.webworker by @Renegade334
  • Loading branch information
Renegade334 authored Aug 21, 2024
1 parent 4271577 commit a5e8efd
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 50 deletions.
95 changes: 47 additions & 48 deletions types/node/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions types/node/node-tests-webworker.ts
Original file line number Diff line number Diff line change
@@ -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";
2 changes: 1 addition & 1 deletion types/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
10 changes: 10 additions & 0 deletions types/node/test/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
3 changes: 2 additions & 1 deletion types/node/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 22 additions & 0 deletions types/node/tsconfig.webworker.json
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit a5e8efd

Please sign in to comment.