diff --git a/types/node/globals.d.ts b/types/node/globals.d.ts index 143ba4ea0bee09..af46ec15a3fb63 100644 --- a/types/node/globals.d.ts +++ b/types/node/globals.d.ts @@ -1,367 +1,168 @@ -export {}; // Make this a module +declare var global: typeof globalThis; -// #region Fetch and friends -// Conditional type aliases, used at the end of this file. -// 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; -type _Headers = typeof globalThis extends { onmessage: any } ? {} : import("undici-types").Headers; -type _MessageEvent = typeof globalThis extends { onmessage: any } ? {} : import("undici-types").MessageEvent; -type _RequestInit = typeof globalThis extends { onmessage: any } ? {} - : import("undici-types").RequestInit; -type _ResponseInit = typeof globalThis extends { onmessage: any } ? {} - : import("undici-types").ResponseInit; -type _WebSocket = typeof globalThis extends { onmessage: any } ? {} : import("undici-types").WebSocket; -type _EventSource = typeof globalThis extends { onmessage: any } ? {} : import("undici-types").EventSource; -type _CloseEvent = typeof globalThis extends { onmessage: any } ? {} : import("undici-types").CloseEvent; -// #endregion Fetch and friends - -// Conditional type definitions for webstorage interface, which conflicts with lib.dom otherwise. -type _Storage = typeof globalThis extends { onabort: any } ? {} : { - readonly length: number; - clear(): void; - getItem(key: string): string | null; - key(index: number): string | null; - removeItem(key: string): void; - setItem(key: string, value: string): void; - [key: string]: any; -}; - -// #region DOMException -type _DOMException = typeof globalThis extends { onmessage: any } ? {} : NodeDOMException; -interface NodeDOMException extends Error { - readonly code: number; - readonly message: string; - readonly name: string; - readonly INDEX_SIZE_ERR: 1; - readonly DOMSTRING_SIZE_ERR: 2; - readonly HIERARCHY_REQUEST_ERR: 3; - readonly WRONG_DOCUMENT_ERR: 4; - readonly INVALID_CHARACTER_ERR: 5; - readonly NO_DATA_ALLOWED_ERR: 6; - readonly NO_MODIFICATION_ALLOWED_ERR: 7; - readonly NOT_FOUND_ERR: 8; - readonly NOT_SUPPORTED_ERR: 9; - readonly INUSE_ATTRIBUTE_ERR: 10; - readonly INVALID_STATE_ERR: 11; - readonly SYNTAX_ERR: 12; - readonly INVALID_MODIFICATION_ERR: 13; - readonly NAMESPACE_ERR: 14; - readonly INVALID_ACCESS_ERR: 15; - readonly VALIDATION_ERR: 16; - readonly TYPE_MISMATCH_ERR: 17; - readonly SECURITY_ERR: 18; - readonly NETWORK_ERR: 19; - readonly ABORT_ERR: 20; - readonly URL_MISMATCH_ERR: 21; - readonly QUOTA_EXCEEDED_ERR: 22; - readonly TIMEOUT_ERR: 23; - readonly INVALID_NODE_TYPE_ERR: 24; - readonly DATA_CLONE_ERR: 25; -} -interface NodeDOMExceptionConstructor { - prototype: DOMException; - new(message?: string, nameOrOptions?: string | { name?: string; cause?: unknown }): DOMException; - readonly INDEX_SIZE_ERR: 1; - readonly DOMSTRING_SIZE_ERR: 2; - readonly HIERARCHY_REQUEST_ERR: 3; - readonly WRONG_DOCUMENT_ERR: 4; - readonly INVALID_CHARACTER_ERR: 5; - readonly NO_DATA_ALLOWED_ERR: 6; - readonly NO_MODIFICATION_ALLOWED_ERR: 7; - readonly NOT_FOUND_ERR: 8; - readonly NOT_SUPPORTED_ERR: 9; - readonly INUSE_ATTRIBUTE_ERR: 10; - readonly INVALID_STATE_ERR: 11; - readonly SYNTAX_ERR: 12; - readonly INVALID_MODIFICATION_ERR: 13; - readonly NAMESPACE_ERR: 14; - readonly INVALID_ACCESS_ERR: 15; - readonly VALIDATION_ERR: 16; - readonly TYPE_MISMATCH_ERR: 17; - readonly SECURITY_ERR: 18; - readonly NETWORK_ERR: 19; - readonly ABORT_ERR: 20; - readonly URL_MISMATCH_ERR: 21; - readonly QUOTA_EXCEEDED_ERR: 22; - readonly TIMEOUT_ERR: 23; - readonly INVALID_NODE_TYPE_ERR: 24; - readonly DATA_CLONE_ERR: 25; -} -// #endregion DOMException - -declare global { - var global: typeof globalThis; - - var process: NodeJS.Process; - var console: Console; - - interface ErrorConstructor { - /** - * Creates a `.stack` property on `targetObject`, which when accessed returns - * a string representing the location in the code at which - * `Error.captureStackTrace()` was called. - * - * ```js - * const myObject = {}; - * Error.captureStackTrace(myObject); - * myObject.stack; // Similar to `new Error().stack` - * ``` - * - * The first line of the trace will be prefixed with - * `${myObject.name}: ${myObject.message}`. - * - * The optional `constructorOpt` argument accepts a function. If given, all frames - * above `constructorOpt`, including `constructorOpt`, will be omitted from the - * generated stack trace. - * - * The `constructorOpt` argument is useful for hiding implementation - * details of error generation from the user. For instance: - * - * ```js - * function a() { - * b(); - * } - * - * function b() { - * c(); - * } - * - * function c() { - * // Create an error without stack trace to avoid calculating the stack trace twice. - * const { stackTraceLimit } = Error; - * Error.stackTraceLimit = 0; - * const error = new Error(); - * Error.stackTraceLimit = stackTraceLimit; - * - * // Capture the stack trace above function b - * Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace - * throw error; - * } - * - * a(); - * ``` - */ - captureStackTrace(targetObject: object, constructorOpt?: Function): void; - /** - * @see https://v8.dev/docs/stack-trace-api#customizing-stack-traces - */ - prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any; - /** - * The `Error.stackTraceLimit` property specifies the number of stack frames - * collected by a stack trace (whether generated by `new Error().stack` or - * `Error.captureStackTrace(obj)`). - * - * The default value is `10` but may be set to any valid JavaScript number. Changes - * will affect any stack trace captured _after_ the value has been changed. - * - * If set to a non-number value, or set to a negative number, stack traces will - * not capture any frames. - */ - stackTraceLimit: number; - } +declare var process: NodeJS.Process; +declare var console: Console; +interface ErrorConstructor { /** - * Enable this API with the `--expose-gc` CLI flag. + * Creates a `.stack` property on `targetObject`, which when accessed returns + * a string representing the location in the code at which + * `Error.captureStackTrace()` was called. + * + * ```js + * const myObject = {}; + * Error.captureStackTrace(myObject); + * myObject.stack; // Similar to `new Error().stack` + * ``` + * + * The first line of the trace will be prefixed with + * `${myObject.name}: ${myObject.message}`. + * + * The optional `constructorOpt` argument accepts a function. If given, all frames + * above `constructorOpt`, including `constructorOpt`, will be omitted from the + * generated stack trace. + * + * The `constructorOpt` argument is useful for hiding implementation + * details of error generation from the user. For instance: + * + * ```js + * function a() { + * b(); + * } + * + * function b() { + * c(); + * } + * + * function c() { + * // Create an error without stack trace to avoid calculating the stack trace twice. + * const { stackTraceLimit } = Error; + * Error.stackTraceLimit = 0; + * const error = new Error(); + * Error.stackTraceLimit = stackTraceLimit; + * + * // Capture the stack trace above function b + * Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace + * throw error; + * } + * + * a(); + * ``` */ - var gc: NodeJS.GCFunction | undefined; - - namespace NodeJS { - interface CallSite { - getColumnNumber(): number | null; - getEnclosingColumnNumber(): number | null; - getEnclosingLineNumber(): number | null; - getEvalOrigin(): string | undefined; - getFileName(): string | null; - getFunction(): Function | undefined; - getFunctionName(): string | null; - getLineNumber(): number | null; - getMethodName(): string | null; - getPosition(): number; - getPromiseIndex(): number | null; - getScriptHash(): string; - getScriptNameOrSourceURL(): string | null; - getThis(): unknown; - getTypeName(): string | null; - isAsync(): boolean; - isConstructor(): boolean; - isEval(): boolean; - isNative(): boolean; - isPromiseAll(): boolean; - isToplevel(): boolean; - } - - interface ErrnoException extends Error { - errno?: number | undefined; - code?: string | undefined; - path?: string | undefined; - syscall?: string | undefined; - } - - interface ReadableStream extends EventEmitter { - readable: boolean; - read(size?: number): string | Buffer; - setEncoding(encoding: BufferEncoding): this; - pause(): this; - resume(): this; - isPaused(): boolean; - pipe(destination: T, options?: { end?: boolean | undefined }): T; - unpipe(destination?: WritableStream): this; - unshift(chunk: string | Uint8Array, encoding?: BufferEncoding): void; - wrap(oldStream: ReadableStream): this; - [Symbol.asyncIterator](): AsyncIterableIterator; - } - - interface WritableStream extends EventEmitter { - writable: boolean; - write(buffer: Uint8Array | string, cb?: (err?: Error | null) => void): boolean; - write(str: string, encoding?: BufferEncoding, cb?: (err?: Error | null) => void): boolean; - end(cb?: () => void): this; - end(data: string | Uint8Array, cb?: () => void): this; - end(str: string, encoding?: BufferEncoding, cb?: () => void): this; - } - - interface ReadWriteStream extends ReadableStream, WritableStream {} - - interface RefCounted { - ref(): this; - unref(): this; - } - - interface Dict { - [key: string]: T | undefined; - } - - interface ReadOnlyDict { - readonly [key: string]: T | undefined; - } - - interface GCFunction { - (minor?: boolean): void; - (options: NodeJS.GCOptions & { execution: "async" }): Promise; - (options: NodeJS.GCOptions): void; - } - - interface GCOptions { - execution?: "sync" | "async" | undefined; - flavor?: "regular" | "last-resort" | undefined; - type?: "major-snapshot" | "major" | "minor" | undefined; - filename?: string | undefined; - } - - /** An iterable iterator returned by the Node.js API. */ - interface Iterator extends IteratorObject { - [Symbol.iterator](): NodeJS.Iterator; - } + captureStackTrace(targetObject: object, constructorOpt?: Function): void; + /** + * @see https://v8.dev/docs/stack-trace-api#customizing-stack-traces + */ + prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any; + /** + * The `Error.stackTraceLimit` property specifies the number of stack frames + * collected by a stack trace (whether generated by `new Error().stack` or + * `Error.captureStackTrace(obj)`). + * + * The default value is `10` but may be set to any valid JavaScript number. Changes + * will affect any stack trace captured _after_ the value has been changed. + * + * If set to a non-number value, or set to a negative number, stack traces will + * not capture any frames. + */ + stackTraceLimit: number; +} - /** An async iterable iterator returned by the Node.js API. */ - interface AsyncIterator extends AsyncIteratorObject { - [Symbol.asyncIterator](): NodeJS.AsyncIterator; - } +/** + * Enable this API with the `--expose-gc` CLI flag. + */ +declare var gc: NodeJS.GCFunction | undefined; + +declare namespace NodeJS { + interface CallSite { + getColumnNumber(): number | null; + getEnclosingColumnNumber(): number | null; + getEnclosingLineNumber(): number | null; + getEvalOrigin(): string | undefined; + getFileName(): string | null; + getFunction(): Function | undefined; + getFunctionName(): string | null; + getLineNumber(): number | null; + getMethodName(): string | null; + getPosition(): number; + getPromiseIndex(): number | null; + getScriptHash(): string; + getScriptNameOrSourceURL(): string | null; + getThis(): unknown; + getTypeName(): string | null; + isAsync(): boolean; + isConstructor(): boolean; + isEval(): boolean; + isNative(): boolean; + isPromiseAll(): boolean; + isToplevel(): boolean; } - // Global DOM types - - interface DOMException extends _DOMException {} - var DOMException: typeof globalThis extends { onmessage: any; DOMException: infer T } ? T - : NodeDOMExceptionConstructor; - - // #region AbortController - interface AbortController { - readonly signal: AbortSignal; - abort(reason?: any): void; + interface ErrnoException extends Error { + errno?: number | undefined; + code?: string | undefined; + path?: string | undefined; + syscall?: string | undefined; } - var AbortController: typeof globalThis extends { onmessage: any; AbortController: infer T } ? T - : { - prototype: AbortController; - new(): AbortController; - }; - interface AbortSignal extends EventTarget { - readonly aborted: boolean; - onabort: ((this: AbortSignal, ev: Event) => any) | null; - readonly reason: any; - throwIfAborted(): void; + interface ReadableStream extends EventEmitter { + readable: boolean; + read(size?: number): string | Buffer; + setEncoding(encoding: BufferEncoding): this; + pause(): this; + resume(): this; + isPaused(): boolean; + pipe(destination: T, options?: { end?: boolean | undefined }): T; + unpipe(destination?: WritableStream): this; + unshift(chunk: string | Uint8Array, encoding?: BufferEncoding): void; + wrap(oldStream: ReadableStream): this; + [Symbol.asyncIterator](): AsyncIterableIterator; } - var AbortSignal: typeof globalThis extends { onmessage: any; AbortSignal: infer T } ? T - : { - prototype: AbortSignal; - new(): AbortSignal; - abort(reason?: any): AbortSignal; - any(signals: AbortSignal[]): AbortSignal; - timeout(milliseconds: number): AbortSignal; - }; - // #endregion AbortController - - // #region Storage - interface Storage extends _Storage {} - // 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; - }; - - var localStorage: Storage; - var sessionStorage: Storage; - // #endregion Storage - - // #region fetch - interface RequestInit extends _RequestInit {} - function fetch( - input: string | URL | globalThis.Request, - init?: RequestInit, - ): Promise; - - interface Request extends _Request {} - var Request: typeof globalThis extends { - onmessage: any; - Request: infer T; - } ? T - : typeof import("undici-types").Request; + interface WritableStream extends EventEmitter { + writable: boolean; + write(buffer: Uint8Array | string, cb?: (err?: Error | null) => void): boolean; + write(str: string, encoding?: BufferEncoding, cb?: (err?: Error | null) => void): boolean; + end(cb?: () => void): this; + end(data: string | Uint8Array, cb?: () => void): this; + end(str: string, encoding?: BufferEncoding, cb?: () => void): this; + } - interface ResponseInit extends _ResponseInit {} + interface ReadWriteStream extends ReadableStream, WritableStream {} - interface Response extends _Response {} - var Response: typeof globalThis extends { - onmessage: any; - Response: infer T; - } ? T - : typeof import("undici-types").Response; + interface RefCounted { + ref(): this; + unref(): this; + } - interface FormData extends _FormData {} - var FormData: typeof globalThis extends { - onmessage: any; - FormData: infer T; - } ? T - : typeof import("undici-types").FormData; + interface Dict { + [key: string]: T | undefined; + } - interface Headers extends _Headers {} - var Headers: typeof globalThis extends { - onmessage: any; - Headers: infer T; - } ? T - : typeof import("undici-types").Headers; + interface ReadOnlyDict { + readonly [key: string]: T | undefined; + } - interface MessageEvent extends _MessageEvent {} - var MessageEvent: typeof globalThis extends { - onmessage: any; - MessageEvent: infer T; - } ? T - : typeof import("undici-types").MessageEvent; + interface GCFunction { + (minor?: boolean): void; + (options: NodeJS.GCOptions & { execution: "async" }): Promise; + (options: NodeJS.GCOptions): void; + } - interface WebSocket extends _WebSocket {} - var WebSocket: typeof globalThis extends { onmessage: any; WebSocket: infer T } ? T - : typeof import("undici-types").WebSocket; + interface GCOptions { + execution?: "sync" | "async" | undefined; + flavor?: "regular" | "last-resort" | undefined; + type?: "major-snapshot" | "major" | "minor" | undefined; + filename?: string | undefined; + } - interface EventSource extends _EventSource {} - var EventSource: typeof globalThis extends { onmessage: any; EventSource: infer T } ? T - : typeof import("undici-types").EventSource; + /** An iterable iterator returned by the Node.js API. */ + interface Iterator extends IteratorObject { + [Symbol.iterator](): NodeJS.Iterator; + } - interface CloseEvent extends _CloseEvent {} - var CloseEvent: typeof globalThis extends { onmessage: any; CloseEvent: infer T } ? T - : typeof import("undici-types").CloseEvent; - // #endregion fetch + /** An async iterable iterator returned by the Node.js API. */ + interface AsyncIterator extends AsyncIteratorObject { + [Symbol.asyncIterator](): NodeJS.AsyncIterator; + } } diff --git a/types/node/index.d.ts b/types/node/index.d.ts index 3b005c11450f06..6c48c808a34dc5 100644 --- a/types/node/index.d.ts +++ b/types/node/index.d.ts @@ -38,6 +38,12 @@ // Definitions for Node.js modules that are not specific to any version of TypeScript: /// +/// +/// +/// +/// +/// +/// /// /// /// @@ -53,7 +59,6 @@ /// /// /// -/// /// /// /// diff --git a/types/node/test/globals.ts b/types/node/test/globals.ts index 27b0010e382602..e8432efea382d0 100644 --- a/types/node/test/globals.ts +++ b/types/node/test/globals.ts @@ -82,6 +82,14 @@ declare var RANDOM_GLOBAL_VARIABLE: true; e.name; // $ExpectType string } +{ + navigator.hardwareConcurrency; // $ExpectType number + navigator.language; // $ExpectType string + navigator.languages; // $ExpectType readonly string[] + navigator.platform; // $ExpectType string + navigator.userAgent; // $ExpectType string; +} + { // @ts-expect-error The pseudoglobal `NodeJS` namespace should not be addressable outside ambient contexts NodeJS; diff --git a/types/node/ts5.6/index.d.ts b/types/node/ts5.6/index.d.ts index b98cc67db600b4..47c03b5444acbf 100644 --- a/types/node/ts5.6/index.d.ts +++ b/types/node/ts5.6/index.d.ts @@ -40,6 +40,12 @@ // Definitions for Node.js modules that are not specific to any version of TypeScript: /// +/// +/// +/// +/// +/// +/// /// /// /// @@ -55,7 +61,6 @@ /// /// /// -/// /// /// /// diff --git a/types/node/ts5.7/index.d.ts b/types/node/ts5.7/index.d.ts index 9793c72ea8a0ae..b23e5d4a98f260 100644 --- a/types/node/ts5.7/index.d.ts +++ b/types/node/ts5.7/index.d.ts @@ -40,6 +40,12 @@ // Definitions for Node.js modules that are not specific to any version of TypeScript: /// +/// +/// +/// +/// +/// +/// /// /// /// @@ -55,7 +61,6 @@ /// /// /// -/// /// /// /// diff --git a/types/node/v18/globals.d.ts b/types/node/v18/globals.d.ts index 389c7058bcd864..d8173404e07baf 100644 --- a/types/node/v18/globals.d.ts +++ b/types/node/v18/globals.d.ts @@ -1,328 +1,168 @@ -export {}; // Make this a module +declare var global: typeof globalThis; -// #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. -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; -type _Headers = typeof globalThis extends { onmessage: any } ? {} : import("undici-types").Headers; -type _MessageEvent = typeof globalThis extends { onmessage: any } ? {} : import("undici-types").MessageEvent; -type _RequestInit = typeof globalThis extends { onmessage: any } ? {} - : import("undici-types").RequestInit; -type _ResponseInit = typeof globalThis extends { onmessage: any } ? {} - : import("undici-types").ResponseInit; -// #endregion Fetch and friends - -// #region DOMException -type _DOMException = typeof globalThis extends { onmessage: any } ? {} : NodeDOMException; -interface NodeDOMException extends Error { - readonly code: number; - readonly message: string; - readonly name: string; - readonly INDEX_SIZE_ERR: 1; - readonly DOMSTRING_SIZE_ERR: 2; - readonly HIERARCHY_REQUEST_ERR: 3; - readonly WRONG_DOCUMENT_ERR: 4; - readonly INVALID_CHARACTER_ERR: 5; - readonly NO_DATA_ALLOWED_ERR: 6; - readonly NO_MODIFICATION_ALLOWED_ERR: 7; - readonly NOT_FOUND_ERR: 8; - readonly NOT_SUPPORTED_ERR: 9; - readonly INUSE_ATTRIBUTE_ERR: 10; - readonly INVALID_STATE_ERR: 11; - readonly SYNTAX_ERR: 12; - readonly INVALID_MODIFICATION_ERR: 13; - readonly NAMESPACE_ERR: 14; - readonly INVALID_ACCESS_ERR: 15; - readonly VALIDATION_ERR: 16; - readonly TYPE_MISMATCH_ERR: 17; - readonly SECURITY_ERR: 18; - readonly NETWORK_ERR: 19; - readonly ABORT_ERR: 20; - readonly URL_MISMATCH_ERR: 21; - readonly QUOTA_EXCEEDED_ERR: 22; - readonly TIMEOUT_ERR: 23; - readonly INVALID_NODE_TYPE_ERR: 24; - readonly DATA_CLONE_ERR: 25; -} -interface NodeDOMExceptionConstructor { - prototype: DOMException; - new(message?: string, nameOrOptions?: string | { name?: string; cause?: unknown }): DOMException; - readonly INDEX_SIZE_ERR: 1; - readonly DOMSTRING_SIZE_ERR: 2; - readonly HIERARCHY_REQUEST_ERR: 3; - readonly WRONG_DOCUMENT_ERR: 4; - readonly INVALID_CHARACTER_ERR: 5; - readonly NO_DATA_ALLOWED_ERR: 6; - readonly NO_MODIFICATION_ALLOWED_ERR: 7; - readonly NOT_FOUND_ERR: 8; - readonly NOT_SUPPORTED_ERR: 9; - readonly INUSE_ATTRIBUTE_ERR: 10; - readonly INVALID_STATE_ERR: 11; - readonly SYNTAX_ERR: 12; - readonly INVALID_MODIFICATION_ERR: 13; - readonly NAMESPACE_ERR: 14; - readonly INVALID_ACCESS_ERR: 15; - readonly VALIDATION_ERR: 16; - readonly TYPE_MISMATCH_ERR: 17; - readonly SECURITY_ERR: 18; - readonly NETWORK_ERR: 19; - readonly ABORT_ERR: 20; - readonly URL_MISMATCH_ERR: 21; - readonly QUOTA_EXCEEDED_ERR: 22; - readonly TIMEOUT_ERR: 23; - readonly INVALID_NODE_TYPE_ERR: 24; - readonly DATA_CLONE_ERR: 25; -} -// #endregion DOMException - -declare global { - var global: typeof globalThis; - - var process: NodeJS.Process; - var console: Console; - - interface ErrorConstructor { - /** - * Creates a `.stack` property on `targetObject`, which when accessed returns - * a string representing the location in the code at which - * `Error.captureStackTrace()` was called. - * - * ```js - * const myObject = {}; - * Error.captureStackTrace(myObject); - * myObject.stack; // Similar to `new Error().stack` - * ``` - * - * The first line of the trace will be prefixed with - * `${myObject.name}: ${myObject.message}`. - * - * The optional `constructorOpt` argument accepts a function. If given, all frames - * above `constructorOpt`, including `constructorOpt`, will be omitted from the - * generated stack trace. - * - * The `constructorOpt` argument is useful for hiding implementation - * details of error generation from the user. For instance: - * - * ```js - * function a() { - * b(); - * } - * - * function b() { - * c(); - * } - * - * function c() { - * // Create an error without stack trace to avoid calculating the stack trace twice. - * const { stackTraceLimit } = Error; - * Error.stackTraceLimit = 0; - * const error = new Error(); - * Error.stackTraceLimit = stackTraceLimit; - * - * // Capture the stack trace above function b - * Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace - * throw error; - * } - * - * a(); - * ``` - */ - captureStackTrace(targetObject: object, constructorOpt?: Function): void; - /** - * @see https://v8.dev/docs/stack-trace-api#customizing-stack-traces - */ - prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any; - /** - * The `Error.stackTraceLimit` property specifies the number of stack frames - * collected by a stack trace (whether generated by `new Error().stack` or - * `Error.captureStackTrace(obj)`). - * - * The default value is `10` but may be set to any valid JavaScript number. Changes - * will affect any stack trace captured _after_ the value has been changed. - * - * If set to a non-number value, or set to a negative number, stack traces will - * not capture any frames. - */ - stackTraceLimit: number; - } +declare var process: NodeJS.Process; +declare var console: Console; +interface ErrorConstructor { /** - * Enable this API with the `--expose-gc` CLI flag. + * Creates a `.stack` property on `targetObject`, which when accessed returns + * a string representing the location in the code at which + * `Error.captureStackTrace()` was called. + * + * ```js + * const myObject = {}; + * Error.captureStackTrace(myObject); + * myObject.stack; // Similar to `new Error().stack` + * ``` + * + * The first line of the trace will be prefixed with + * `${myObject.name}: ${myObject.message}`. + * + * The optional `constructorOpt` argument accepts a function. If given, all frames + * above `constructorOpt`, including `constructorOpt`, will be omitted from the + * generated stack trace. + * + * The `constructorOpt` argument is useful for hiding implementation + * details of error generation from the user. For instance: + * + * ```js + * function a() { + * b(); + * } + * + * function b() { + * c(); + * } + * + * function c() { + * // Create an error without stack trace to avoid calculating the stack trace twice. + * const { stackTraceLimit } = Error; + * Error.stackTraceLimit = 0; + * const error = new Error(); + * Error.stackTraceLimit = stackTraceLimit; + * + * // Capture the stack trace above function b + * Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace + * throw error; + * } + * + * a(); + * ``` */ - var gc: NodeJS.GCFunction | undefined; - - namespace NodeJS { - interface CallSite { - getColumnNumber(): number | null; - getEnclosingColumnNumber(): number | null; - getEnclosingLineNumber(): number | null; - getEvalOrigin(): string | undefined; - getFileName(): string | null; - getFunction(): Function | undefined; - getFunctionName(): string | null; - getLineNumber(): number | null; - getMethodName(): string | null; - getPosition(): number; - getPromiseIndex(): number | null; - getScriptHash(): string; - getScriptNameOrSourceURL(): string | null; - getThis(): unknown; - getTypeName(): string | null; - isAsync(): boolean; - isConstructor(): boolean; - isEval(): boolean; - isNative(): boolean; - isPromiseAll(): boolean; - isToplevel(): boolean; - } - - interface ErrnoException extends Error { - errno?: number | undefined; - code?: string | undefined; - path?: string | undefined; - syscall?: string | undefined; - } - - interface ReadableStream extends EventEmitter { - readable: boolean; - read(size?: number): string | Buffer; - setEncoding(encoding: BufferEncoding): this; - pause(): this; - resume(): this; - isPaused(): boolean; - pipe(destination: T, options?: { end?: boolean | undefined }): T; - unpipe(destination?: WritableStream): this; - unshift(chunk: string | Uint8Array, encoding?: BufferEncoding): void; - wrap(oldStream: ReadableStream): this; - [Symbol.asyncIterator](): AsyncIterableIterator; - } - - interface WritableStream extends EventEmitter { - writable: boolean; - write(buffer: Uint8Array | string, cb?: (err?: Error | null) => void): boolean; - write(str: string, encoding?: BufferEncoding, cb?: (err?: Error | null) => void): boolean; - end(cb?: () => void): this; - end(data: string | Uint8Array, cb?: () => void): this; - end(str: string, encoding?: BufferEncoding, cb?: () => void): this; - } - - interface ReadWriteStream extends ReadableStream, WritableStream {} - - interface RefCounted { - ref(): this; - unref(): this; - } - - interface Dict { - [key: string]: T | undefined; - } - - interface ReadOnlyDict { - readonly [key: string]: T | undefined; - } - - interface GCFunction { - (minor?: boolean): void; - (options: NodeJS.GCOptions & { execution: "async" }): Promise; - (options: NodeJS.GCOptions): void; - } - - interface GCOptions { - execution?: "sync" | "async" | undefined; - type?: "major" | "minor" | undefined; - } - - /** An iterable iterator returned by the Node.js API. */ - // Default TReturn/TNext in v18 is `any`, for compatibility with the previously-used IterableIterator. - interface Iterator extends IteratorObject { - [Symbol.iterator](): NodeJS.Iterator; - } + captureStackTrace(targetObject: object, constructorOpt?: Function): void; + /** + * @see https://v8.dev/docs/stack-trace-api#customizing-stack-traces + */ + prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any; + /** + * The `Error.stackTraceLimit` property specifies the number of stack frames + * collected by a stack trace (whether generated by `new Error().stack` or + * `Error.captureStackTrace(obj)`). + * + * The default value is `10` but may be set to any valid JavaScript number. Changes + * will affect any stack trace captured _after_ the value has been changed. + * + * If set to a non-number value, or set to a negative number, stack traces will + * not capture any frames. + */ + stackTraceLimit: number; +} - /** An async iterable iterator returned by the Node.js API. */ - // Default TReturn/TNext in v18 is `any`, for compatibility with the previously-used AsyncIterableIterator. - interface AsyncIterator extends AsyncIteratorObject { - [Symbol.asyncIterator](): NodeJS.AsyncIterator; - } +/** + * Enable this API with the `--expose-gc` CLI flag. + */ +declare var gc: NodeJS.GCFunction | undefined; + +declare namespace NodeJS { + interface CallSite { + getColumnNumber(): number | null; + getEnclosingColumnNumber(): number | null; + getEnclosingLineNumber(): number | null; + getEvalOrigin(): string | undefined; + getFileName(): string | null; + getFunction(): Function | undefined; + getFunctionName(): string | null; + getLineNumber(): number | null; + getMethodName(): string | null; + getPosition(): number; + getPromiseIndex(): number | null; + getScriptHash(): string; + getScriptNameOrSourceURL(): string | null; + getThis(): unknown; + getTypeName(): string | null; + isAsync(): boolean; + isConstructor(): boolean; + isEval(): boolean; + isNative(): boolean; + isPromiseAll(): boolean; + isToplevel(): boolean; } - // Global DOM types - - interface DOMException extends _DOMException {} - var DOMException: typeof globalThis extends { onmessage: any; DOMException: infer T } ? T - : NodeDOMExceptionConstructor; + interface ErrnoException extends Error { + errno?: number | undefined; + code?: string | undefined; + path?: string | undefined; + syscall?: string | undefined; + } - // #region AbortController - interface AbortController { - readonly signal: AbortSignal; - abort(reason?: any): void; + interface ReadableStream extends EventEmitter { + readable: boolean; + read(size?: number): string | Buffer; + setEncoding(encoding: BufferEncoding): this; + pause(): this; + resume(): this; + isPaused(): boolean; + pipe(destination: T, options?: { end?: boolean | undefined }): T; + unpipe(destination?: WritableStream): this; + unshift(chunk: string | Uint8Array, encoding?: BufferEncoding): void; + wrap(oldStream: ReadableStream): this; + [Symbol.asyncIterator](): AsyncIterableIterator; } - var AbortController: typeof globalThis extends { onmessage: any; AbortController: infer T } ? T - : { - prototype: AbortController; - new(): AbortController; - }; - interface AbortSignal extends EventTarget { - readonly aborted: boolean; - onabort: ((this: AbortSignal, ev: Event) => any) | null; - readonly reason: any; - throwIfAborted(): void; + interface WritableStream extends EventEmitter { + writable: boolean; + write(buffer: Uint8Array | string, cb?: (err?: Error | null) => void): boolean; + write(str: string, encoding?: BufferEncoding, cb?: (err?: Error | null) => void): boolean; + end(cb?: () => void): this; + end(data: string | Uint8Array, cb?: () => void): this; + end(str: string, encoding?: BufferEncoding, cb?: () => void): this; } - var AbortSignal: typeof globalThis extends { onmessage: any; AbortSignal: infer T } ? T - : { - prototype: AbortSignal; - new(): AbortSignal; - abort(reason?: any): AbortSignal; - any(signals: AbortSignal[]): AbortSignal; - timeout(milliseconds: number): AbortSignal; - }; - // #endregion AbortController - // #region fetch - interface RequestInit extends _RequestInit {} + interface ReadWriteStream extends ReadableStream, WritableStream {} - function fetch( - input: string | URL | Request, - init?: RequestInit, - ): Promise; + interface RefCounted { + ref(): this; + unref(): this; + } - interface Request extends _Request {} - var Request: typeof globalThis extends { - onmessage: any; - Request: infer T; - } ? T - : typeof import("undici-types").Request; + interface Dict { + [key: string]: T | undefined; + } - interface ResponseInit extends _ResponseInit {} + interface ReadOnlyDict { + readonly [key: string]: T | undefined; + } - interface Response extends _Response {} - var Response: typeof globalThis extends { - onmessage: any; - Response: infer T; - } ? T - : typeof import("undici-types").Response; + interface GCFunction { + (minor?: boolean): void; + (options: NodeJS.GCOptions & { execution: "async" }): Promise; + (options: NodeJS.GCOptions): void; + } - interface FormData extends _FormData {} - var FormData: typeof globalThis extends { - onmessage: any; - FormData: infer T; - } ? T - : typeof import("undici-types").FormData; + interface GCOptions { + execution?: "sync" | "async" | undefined; + type?: "major" | "minor" | undefined; + } - interface Headers extends _Headers {} - var Headers: typeof globalThis extends { - onmessage: any; - Headers: infer T; - } ? T - : typeof import("undici-types").Headers; + /** An iterable iterator returned by the Node.js API. */ + // Default TReturn/TNext in v18 is `any`, for compatibility with the previously-used IterableIterator. + interface Iterator extends IteratorObject { + [Symbol.iterator](): NodeJS.Iterator; + } - interface MessageEvent extends _MessageEvent {} - var MessageEvent: typeof globalThis extends { - onmessage: any; - MessageEvent: infer T; - } ? T - : typeof import("undici-types").MessageEvent; - // #endregion fetch + /** An async iterable iterator returned by the Node.js API. */ + // Default TReturn/TNext in v18 is `any`, for compatibility with the previously-used AsyncIterableIterator. + interface AsyncIterator extends AsyncIteratorObject { + [Symbol.asyncIterator](): NodeJS.AsyncIterator; + } } diff --git a/types/node/v18/index.d.ts b/types/node/v18/index.d.ts index 6d63dbd3740525..9fae42bb32678a 100644 --- a/types/node/v18/index.d.ts +++ b/types/node/v18/index.d.ts @@ -36,6 +36,10 @@ // Definitions for Node.js modules that are not specific to any version of TypeScript: /// +/// +/// +/// +/// /// /// /// @@ -51,7 +55,6 @@ /// /// /// -/// /// /// /// diff --git a/types/node/v18/ts5.6/index.d.ts b/types/node/v18/ts5.6/index.d.ts index fc2021996ae59c..b62e0dccb7fe12 100644 --- a/types/node/v18/ts5.6/index.d.ts +++ b/types/node/v18/ts5.6/index.d.ts @@ -36,6 +36,10 @@ // Definitions for Node.js modules that are not specific to any version of TypeScript: /// +/// +/// +/// +/// /// /// /// @@ -51,7 +55,6 @@ /// /// /// -/// /// /// /// diff --git a/types/node/v18/web-globals/abortcontroller.d.ts b/types/node/v18/web-globals/abortcontroller.d.ts new file mode 100644 index 00000000000000..f36cb9ae5d689f --- /dev/null +++ b/types/node/v18/web-globals/abortcontroller.d.ts @@ -0,0 +1,34 @@ +export {}; + +type _AbortController = typeof globalThis extends { onmessage: any } ? {} : AbortController; +interface AbortController { + readonly signal: AbortSignal; + abort(reason?: any): void; +} + +type _AbortSignal = typeof globalThis extends { onmessage: any } ? {} : AbortSignal; +interface AbortSignal extends EventTarget { + readonly aborted: boolean; + onabort: ((this: AbortSignal, ev: Event) => any) | null; + readonly reason: any; + throwIfAborted(): void; +} + +declare global { + interface AbortController extends _AbortController {} + var AbortController: typeof globalThis extends { onmessage: any; AbortController: infer T } ? T + : { + prototype: AbortController; + new(): AbortController; + }; + + interface AbortSignal extends _AbortSignal {} + var AbortSignal: typeof globalThis extends { onmessage: any; AbortSignal: infer T } ? T + : { + prototype: AbortSignal; + new(): AbortSignal; + abort(reason?: any): AbortSignal; + any(signals: AbortSignal[]): AbortSignal; + timeout(milliseconds: number): AbortSignal; + }; +} diff --git a/types/node/v18/web-globals/domexception.d.ts b/types/node/v18/web-globals/domexception.d.ts new file mode 100644 index 00000000000000..5b1662c4cf2ab9 --- /dev/null +++ b/types/node/v18/web-globals/domexception.d.ts @@ -0,0 +1,68 @@ +export {}; + +type _DOMException = typeof globalThis extends { onmessage: any } ? {} : DOMException; +interface DOMException extends Error { + readonly code: number; + readonly message: string; + readonly name: string; + readonly INDEX_SIZE_ERR: 1; + readonly DOMSTRING_SIZE_ERR: 2; + readonly HIERARCHY_REQUEST_ERR: 3; + readonly WRONG_DOCUMENT_ERR: 4; + readonly INVALID_CHARACTER_ERR: 5; + readonly NO_DATA_ALLOWED_ERR: 6; + readonly NO_MODIFICATION_ALLOWED_ERR: 7; + readonly NOT_FOUND_ERR: 8; + readonly NOT_SUPPORTED_ERR: 9; + readonly INUSE_ATTRIBUTE_ERR: 10; + readonly INVALID_STATE_ERR: 11; + readonly SYNTAX_ERR: 12; + readonly INVALID_MODIFICATION_ERR: 13; + readonly NAMESPACE_ERR: 14; + readonly INVALID_ACCESS_ERR: 15; + readonly VALIDATION_ERR: 16; + readonly TYPE_MISMATCH_ERR: 17; + readonly SECURITY_ERR: 18; + readonly NETWORK_ERR: 19; + readonly ABORT_ERR: 20; + readonly URL_MISMATCH_ERR: 21; + readonly QUOTA_EXCEEDED_ERR: 22; + readonly TIMEOUT_ERR: 23; + readonly INVALID_NODE_TYPE_ERR: 24; + readonly DATA_CLONE_ERR: 25; +} + +declare global { + interface DOMException extends _DOMException {} + var DOMException: typeof globalThis extends { onmessage: any; DOMException: infer T } ? T + : { + prototype: DOMException; + new(message?: string, name?: string): DOMException; + new(message?: string, options?: { name?: string; cause?: unknown }): DOMException; + readonly INDEX_SIZE_ERR: 1; + readonly DOMSTRING_SIZE_ERR: 2; + readonly HIERARCHY_REQUEST_ERR: 3; + readonly WRONG_DOCUMENT_ERR: 4; + readonly INVALID_CHARACTER_ERR: 5; + readonly NO_DATA_ALLOWED_ERR: 6; + readonly NO_MODIFICATION_ALLOWED_ERR: 7; + readonly NOT_FOUND_ERR: 8; + readonly NOT_SUPPORTED_ERR: 9; + readonly INUSE_ATTRIBUTE_ERR: 10; + readonly INVALID_STATE_ERR: 11; + readonly SYNTAX_ERR: 12; + readonly INVALID_MODIFICATION_ERR: 13; + readonly NAMESPACE_ERR: 14; + readonly INVALID_ACCESS_ERR: 15; + readonly VALIDATION_ERR: 16; + readonly TYPE_MISMATCH_ERR: 17; + readonly SECURITY_ERR: 18; + readonly NETWORK_ERR: 19; + readonly ABORT_ERR: 20; + readonly URL_MISMATCH_ERR: 21; + readonly QUOTA_EXCEEDED_ERR: 22; + readonly TIMEOUT_ERR: 23; + readonly INVALID_NODE_TYPE_ERR: 24; + readonly DATA_CLONE_ERR: 25; + }; +} diff --git a/types/node/v18/dom-events.d.ts b/types/node/v18/web-globals/events.d.ts similarity index 76% rename from types/node/v18/dom-events.d.ts rename to types/node/v18/web-globals/events.d.ts index 99665d25d87927..6d39d5f6b3e9a4 100644 --- a/types/node/v18/dom-events.d.ts +++ b/types/node/v18/web-globals/events.d.ts @@ -1,44 +1,31 @@ -// Make this a module export {}; -// Conditional type aliases, which are later merged into the global scope. -// Will either be empty if the relevant web library is already present, or the @types/node definition otherwise. +interface AddEventListenerOptions extends EventListenerOptions { + once?: boolean; + passive?: boolean; + signal?: AbortSignal; +} -type __Event = typeof globalThis extends { onmessage: any } ? {} : Event; +type _Event = typeof globalThis extends { onmessage: any } ? {} : Event; interface Event { readonly bubbles: boolean; cancelBubble: boolean; readonly cancelable: boolean; readonly composed: boolean; - composedPath(): [EventTarget?]; readonly currentTarget: EventTarget | null; readonly defaultPrevented: boolean; readonly eventPhase: 0 | 2; - initEvent(type: string, bubbles?: boolean, cancelable?: boolean): void; readonly isTrusted: boolean; - preventDefault(): void; - readonly returnValue: boolean; + returnValue: boolean; readonly srcElement: EventTarget | null; - stopImmediatePropagation(): void; - stopPropagation(): void; readonly target: EventTarget | null; readonly timeStamp: number; readonly type: string; -} - -type __EventTarget = typeof globalThis extends { onmessage: any } ? {} : EventTarget; -interface EventTarget { - addEventListener( - type: string, - listener: EventListener | EventListenerObject, - options?: AddEventListenerOptions | boolean, - ): void; - dispatchEvent(event: Event): boolean; - removeEventListener( - type: string, - listener: EventListener | EventListenerObject, - options?: EventListenerOptions | boolean, - ): void; + composedPath(): [EventTarget?]; + initEvent(type: string, bubbles?: boolean, cancelable?: boolean): void; + preventDefault(): void; + stopImmediatePropagation(): void; + stopPropagation(): void; } interface EventInit { @@ -47,16 +34,6 @@ interface EventInit { composed?: boolean; } -interface EventListenerOptions { - capture?: boolean; -} - -interface AddEventListenerOptions extends EventListenerOptions { - once?: boolean; - passive?: boolean; - signal?: AbortSignal; -} - interface EventListener { (evt: Event): void; } @@ -65,16 +42,34 @@ interface EventListenerObject { handleEvent(object: Event): void; } -// Merge conditional interfaces into global scope, and conditionally declare global constructors. +interface EventListenerOptions { + capture?: boolean; +} + +type _EventTarget = typeof globalThis extends { onmessage: any } ? {} : EventTarget; +interface EventTarget { + addEventListener( + type: string, + listener: EventListener | EventListenerObject, + options?: AddEventListenerOptions | boolean, + ): void; + dispatchEvent(event: Event): boolean; + removeEventListener( + type: string, + listener: EventListener | EventListenerObject, + options?: EventListenerOptions | boolean, + ): void; +} + declare global { - interface Event extends __Event {} + interface Event extends _Event {} var Event: typeof globalThis extends { onmessage: any; Event: infer T } ? T : { prototype: Event; new(type: string, eventInitDict?: EventInit): Event; }; - interface EventTarget extends __EventTarget {} + interface EventTarget extends _EventTarget {} var EventTarget: typeof globalThis extends { onmessage: any; EventTarget: infer T } ? T : { prototype: EventTarget; diff --git a/types/node/v18/web-globals/fetch.d.ts b/types/node/v18/web-globals/fetch.d.ts new file mode 100644 index 00000000000000..70e57ba72b774d --- /dev/null +++ b/types/node/v18/web-globals/fetch.d.ts @@ -0,0 +1,38 @@ +export {}; + +import * as undici from "undici-types"; + +type _FormData = typeof globalThis extends { onmessage: any } ? {} : undici.FormData; +type _Headers = typeof globalThis extends { onmessage: any } ? {} : undici.Headers; +type _MessageEvent = typeof globalThis extends { onmessage: any } ? {} : undici.MessageEvent; +type _Request = typeof globalThis extends { onmessage: any } ? {} : undici.Request; +type _RequestInit = typeof globalThis extends { onmessage: any } ? {} : undici.RequestInit; +type _Response = typeof globalThis extends { onmessage: any } ? {} : undici.Response; +type _ResponseInit = typeof globalThis extends { onmessage: any } ? {} : undici.ResponseInit; + +declare global { + function fetch( + input: string | URL | Request, + init?: RequestInit, + ): Promise; + + interface FormData extends _FormData {} + var FormData: typeof globalThis extends { onmessage: any; FormData: infer T } ? T : typeof undici.FormData; + + interface Headers extends _Headers {} + var Headers: typeof globalThis extends { onmessage: any; Headers: infer T } ? T : typeof undici.Headers; + + interface MessageEvent extends _MessageEvent {} + var MessageEvent: typeof globalThis extends { onmessage: any; MessageEvent: infer T } ? T + : typeof undici.MessageEvent; + + interface Request extends _Request {} + var Request: typeof globalThis extends { onmessage: any; Request: infer T } ? T : typeof undici.Request; + + interface RequestInit extends _RequestInit {} + + interface Response extends _Response {} + var Response: typeof globalThis extends { onmessage: any; Response: infer T } ? T : typeof undici.Response; + + interface ResponseInit extends _ResponseInit {} +} diff --git a/types/node/v20/globals.d.ts b/types/node/v20/globals.d.ts index ad492d68159a37..70891c4a9e891e 100644 --- a/types/node/v20/globals.d.ts +++ b/types/node/v20/globals.d.ts @@ -1,335 +1,170 @@ -export {}; // Make this a module +declare var global: typeof globalThis; -// #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. -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; -type _Headers = typeof globalThis extends { onmessage: any } ? {} : import("undici-types").Headers; -type _MessageEvent = typeof globalThis extends { onmessage: any } ? {} : import("undici-types").MessageEvent; -type _RequestInit = typeof globalThis extends { onmessage: any } ? {} - : import("undici-types").RequestInit; -type _ResponseInit = typeof globalThis extends { onmessage: any } ? {} - : import("undici-types").ResponseInit; -type _EventSource = typeof globalThis extends { onmessage: any } ? {} : import("undici-types").EventSource; -// #endregion Fetch and friends - -// #region DOMException -type _DOMException = typeof globalThis extends { onmessage: any } ? {} : NodeDOMException; -interface NodeDOMException extends Error { - readonly code: number; - readonly message: string; - readonly name: string; - readonly INDEX_SIZE_ERR: 1; - readonly DOMSTRING_SIZE_ERR: 2; - readonly HIERARCHY_REQUEST_ERR: 3; - readonly WRONG_DOCUMENT_ERR: 4; - readonly INVALID_CHARACTER_ERR: 5; - readonly NO_DATA_ALLOWED_ERR: 6; - readonly NO_MODIFICATION_ALLOWED_ERR: 7; - readonly NOT_FOUND_ERR: 8; - readonly NOT_SUPPORTED_ERR: 9; - readonly INUSE_ATTRIBUTE_ERR: 10; - readonly INVALID_STATE_ERR: 11; - readonly SYNTAX_ERR: 12; - readonly INVALID_MODIFICATION_ERR: 13; - readonly NAMESPACE_ERR: 14; - readonly INVALID_ACCESS_ERR: 15; - readonly VALIDATION_ERR: 16; - readonly TYPE_MISMATCH_ERR: 17; - readonly SECURITY_ERR: 18; - readonly NETWORK_ERR: 19; - readonly ABORT_ERR: 20; - readonly URL_MISMATCH_ERR: 21; - readonly QUOTA_EXCEEDED_ERR: 22; - readonly TIMEOUT_ERR: 23; - readonly INVALID_NODE_TYPE_ERR: 24; - readonly DATA_CLONE_ERR: 25; -} -interface NodeDOMExceptionConstructor { - prototype: DOMException; - new(message?: string, nameOrOptions?: string | { name?: string; cause?: unknown }): DOMException; - readonly INDEX_SIZE_ERR: 1; - readonly DOMSTRING_SIZE_ERR: 2; - readonly HIERARCHY_REQUEST_ERR: 3; - readonly WRONG_DOCUMENT_ERR: 4; - readonly INVALID_CHARACTER_ERR: 5; - readonly NO_DATA_ALLOWED_ERR: 6; - readonly NO_MODIFICATION_ALLOWED_ERR: 7; - readonly NOT_FOUND_ERR: 8; - readonly NOT_SUPPORTED_ERR: 9; - readonly INUSE_ATTRIBUTE_ERR: 10; - readonly INVALID_STATE_ERR: 11; - readonly SYNTAX_ERR: 12; - readonly INVALID_MODIFICATION_ERR: 13; - readonly NAMESPACE_ERR: 14; - readonly INVALID_ACCESS_ERR: 15; - readonly VALIDATION_ERR: 16; - readonly TYPE_MISMATCH_ERR: 17; - readonly SECURITY_ERR: 18; - readonly NETWORK_ERR: 19; - readonly ABORT_ERR: 20; - readonly URL_MISMATCH_ERR: 21; - readonly QUOTA_EXCEEDED_ERR: 22; - readonly TIMEOUT_ERR: 23; - readonly INVALID_NODE_TYPE_ERR: 24; - readonly DATA_CLONE_ERR: 25; -} -// #endregion DOMException - -declare global { - var global: typeof globalThis; - - var process: NodeJS.Process; - var console: Console; - - interface ErrorConstructor { - /** - * Creates a `.stack` property on `targetObject`, which when accessed returns - * a string representing the location in the code at which - * `Error.captureStackTrace()` was called. - * - * ```js - * const myObject = {}; - * Error.captureStackTrace(myObject); - * myObject.stack; // Similar to `new Error().stack` - * ``` - * - * The first line of the trace will be prefixed with - * `${myObject.name}: ${myObject.message}`. - * - * The optional `constructorOpt` argument accepts a function. If given, all frames - * above `constructorOpt`, including `constructorOpt`, will be omitted from the - * generated stack trace. - * - * The `constructorOpt` argument is useful for hiding implementation - * details of error generation from the user. For instance: - * - * ```js - * function a() { - * b(); - * } - * - * function b() { - * c(); - * } - * - * function c() { - * // Create an error without stack trace to avoid calculating the stack trace twice. - * const { stackTraceLimit } = Error; - * Error.stackTraceLimit = 0; - * const error = new Error(); - * Error.stackTraceLimit = stackTraceLimit; - * - * // Capture the stack trace above function b - * Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace - * throw error; - * } - * - * a(); - * ``` - */ - captureStackTrace(targetObject: object, constructorOpt?: Function): void; - /** - * @see https://v8.dev/docs/stack-trace-api#customizing-stack-traces - */ - prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any; - /** - * The `Error.stackTraceLimit` property specifies the number of stack frames - * collected by a stack trace (whether generated by `new Error().stack` or - * `Error.captureStackTrace(obj)`). - * - * The default value is `10` but may be set to any valid JavaScript number. Changes - * will affect any stack trace captured _after_ the value has been changed. - * - * If set to a non-number value, or set to a negative number, stack traces will - * not capture any frames. - */ - stackTraceLimit: number; - } +declare var process: NodeJS.Process; +declare var console: Console; +interface ErrorConstructor { /** - * Enable this API with the `--expose-gc` CLI flag. + * Creates a `.stack` property on `targetObject`, which when accessed returns + * a string representing the location in the code at which + * `Error.captureStackTrace()` was called. + * + * ```js + * const myObject = {}; + * Error.captureStackTrace(myObject); + * myObject.stack; // Similar to `new Error().stack` + * ``` + * + * The first line of the trace will be prefixed with + * `${myObject.name}: ${myObject.message}`. + * + * The optional `constructorOpt` argument accepts a function. If given, all frames + * above `constructorOpt`, including `constructorOpt`, will be omitted from the + * generated stack trace. + * + * The `constructorOpt` argument is useful for hiding implementation + * details of error generation from the user. For instance: + * + * ```js + * function a() { + * b(); + * } + * + * function b() { + * c(); + * } + * + * function c() { + * // Create an error without stack trace to avoid calculating the stack trace twice. + * const { stackTraceLimit } = Error; + * Error.stackTraceLimit = 0; + * const error = new Error(); + * Error.stackTraceLimit = stackTraceLimit; + * + * // Capture the stack trace above function b + * Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace + * throw error; + * } + * + * a(); + * ``` */ - var gc: NodeJS.GCFunction | undefined; - - namespace NodeJS { - interface CallSite { - getColumnNumber(): number | null; - getEnclosingColumnNumber(): number | null; - getEnclosingLineNumber(): number | null; - getEvalOrigin(): string | undefined; - getFileName(): string | null; - getFunction(): Function | undefined; - getFunctionName(): string | null; - getLineNumber(): number | null; - getMethodName(): string | null; - getPosition(): number; - getPromiseIndex(): number | null; - getScriptHash(): string; - getScriptNameOrSourceURL(): string | null; - getThis(): unknown; - getTypeName(): string | null; - isAsync(): boolean; - isConstructor(): boolean; - isEval(): boolean; - isNative(): boolean; - isPromiseAll(): boolean; - isToplevel(): boolean; - } - - interface ErrnoException extends Error { - errno?: number | undefined; - code?: string | undefined; - path?: string | undefined; - syscall?: string | undefined; - } - - interface ReadableStream extends EventEmitter { - readable: boolean; - read(size?: number): string | Buffer; - setEncoding(encoding: BufferEncoding): this; - pause(): this; - resume(): this; - isPaused(): boolean; - pipe(destination: T, options?: { end?: boolean | undefined }): T; - unpipe(destination?: WritableStream): this; - unshift(chunk: string | Uint8Array, encoding?: BufferEncoding): void; - wrap(oldStream: ReadableStream): this; - [Symbol.asyncIterator](): AsyncIterableIterator; - } - - interface WritableStream extends EventEmitter { - writable: boolean; - write(buffer: Uint8Array | string, cb?: (err?: Error | null) => void): boolean; - write(str: string, encoding?: BufferEncoding, cb?: (err?: Error | null) => void): boolean; - end(cb?: () => void): this; - end(data: string | Uint8Array, cb?: () => void): this; - end(str: string, encoding?: BufferEncoding, cb?: () => void): this; - } - - interface ReadWriteStream extends ReadableStream, WritableStream {} - - interface RefCounted { - ref(): this; - unref(): this; - } - - interface Dict { - [key: string]: T | undefined; - } - - interface ReadOnlyDict { - readonly [key: string]: T | undefined; - } - - interface GCFunction { - (minor?: boolean): void; - (options: NodeJS.GCOptions & { execution: "async" }): Promise; - (options: NodeJS.GCOptions): void; - } - - interface GCOptions { - execution?: "sync" | "async" | undefined; - flavor?: "regular" | "last-resort" | undefined; - type?: "major-snapshot" | "major" | "minor" | undefined; - filename?: string | undefined; - } - - /** An iterable iterator returned by the Node.js API. */ - // Default TReturn/TNext in v20 is `any`, for compatibility with the previously-used IterableIterator. - interface Iterator extends IteratorObject { - [Symbol.iterator](): NodeJS.Iterator; - } + captureStackTrace(targetObject: object, constructorOpt?: Function): void; + /** + * @see https://v8.dev/docs/stack-trace-api#customizing-stack-traces + */ + prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any; + /** + * The `Error.stackTraceLimit` property specifies the number of stack frames + * collected by a stack trace (whether generated by `new Error().stack` or + * `Error.captureStackTrace(obj)`). + * + * The default value is `10` but may be set to any valid JavaScript number. Changes + * will affect any stack trace captured _after_ the value has been changed. + * + * If set to a non-number value, or set to a negative number, stack traces will + * not capture any frames. + */ + stackTraceLimit: number; +} - /** An async iterable iterator returned by the Node.js API. */ - // Default TReturn/TNext in v20 is `any`, for compatibility with the previously-used AsyncIterableIterator. - interface AsyncIterator extends AsyncIteratorObject { - [Symbol.asyncIterator](): NodeJS.AsyncIterator; - } +/** + * Enable this API with the `--expose-gc` CLI flag. + */ +declare var gc: NodeJS.GCFunction | undefined; + +declare namespace NodeJS { + interface CallSite { + getColumnNumber(): number | null; + getEnclosingColumnNumber(): number | null; + getEnclosingLineNumber(): number | null; + getEvalOrigin(): string | undefined; + getFileName(): string | null; + getFunction(): Function | undefined; + getFunctionName(): string | null; + getLineNumber(): number | null; + getMethodName(): string | null; + getPosition(): number; + getPromiseIndex(): number | null; + getScriptHash(): string; + getScriptNameOrSourceURL(): string | null; + getThis(): unknown; + getTypeName(): string | null; + isAsync(): boolean; + isConstructor(): boolean; + isEval(): boolean; + isNative(): boolean; + isPromiseAll(): boolean; + isToplevel(): boolean; } - // Global DOM types - - interface DOMException extends _DOMException {} - var DOMException: typeof globalThis extends { onmessage: any; DOMException: infer T } ? T - : NodeDOMExceptionConstructor; - - // #region AbortController - interface AbortController { - readonly signal: AbortSignal; - abort(reason?: any): void; + interface ErrnoException extends Error { + errno?: number | undefined; + code?: string | undefined; + path?: string | undefined; + syscall?: string | undefined; } - var AbortController: typeof globalThis extends { onmessage: any; AbortController: infer T } ? T - : { - prototype: AbortController; - new(): AbortController; - }; - interface AbortSignal extends EventTarget { - readonly aborted: boolean; - onabort: ((this: AbortSignal, ev: Event) => any) | null; - readonly reason: any; - throwIfAborted(): void; + interface ReadableStream extends EventEmitter { + readable: boolean; + read(size?: number): string | Buffer; + setEncoding(encoding: BufferEncoding): this; + pause(): this; + resume(): this; + isPaused(): boolean; + pipe(destination: T, options?: { end?: boolean | undefined }): T; + unpipe(destination?: WritableStream): this; + unshift(chunk: string | Uint8Array, encoding?: BufferEncoding): void; + wrap(oldStream: ReadableStream): this; + [Symbol.asyncIterator](): AsyncIterableIterator; } - var AbortSignal: typeof globalThis extends { onmessage: any; AbortSignal: infer T } ? T - : { - prototype: AbortSignal; - new(): AbortSignal; - abort(reason?: any): AbortSignal; - any(signals: AbortSignal[]): AbortSignal; - timeout(milliseconds: number): AbortSignal; - }; - // #endregion AbortController - // #region fetch - interface RequestInit extends _RequestInit {} + interface WritableStream extends EventEmitter { + writable: boolean; + write(buffer: Uint8Array | string, cb?: (err?: Error | null) => void): boolean; + write(str: string, encoding?: BufferEncoding, cb?: (err?: Error | null) => void): boolean; + end(cb?: () => void): this; + end(data: string | Uint8Array, cb?: () => void): this; + end(str: string, encoding?: BufferEncoding, cb?: () => void): this; + } - function fetch( - input: string | URL | globalThis.Request, - init?: RequestInit, - ): Promise; + interface ReadWriteStream extends ReadableStream, WritableStream {} - interface Request extends _Request {} - var Request: typeof globalThis extends { - onmessage: any; - Request: infer T; - } ? T - : typeof import("undici-types").Request; + interface RefCounted { + ref(): this; + unref(): this; + } - interface ResponseInit extends _ResponseInit {} + interface Dict { + [key: string]: T | undefined; + } - interface Response extends _Response {} - var Response: typeof globalThis extends { - onmessage: any; - Response: infer T; - } ? T - : typeof import("undici-types").Response; + interface ReadOnlyDict { + readonly [key: string]: T | undefined; + } - interface FormData extends _FormData {} - var FormData: typeof globalThis extends { - onmessage: any; - FormData: infer T; - } ? T - : typeof import("undici-types").FormData; + interface GCFunction { + (minor?: boolean): void; + (options: NodeJS.GCOptions & { execution: "async" }): Promise; + (options: NodeJS.GCOptions): void; + } - interface Headers extends _Headers {} - var Headers: typeof globalThis extends { - onmessage: any; - Headers: infer T; - } ? T - : typeof import("undici-types").Headers; + interface GCOptions { + execution?: "sync" | "async" | undefined; + flavor?: "regular" | "last-resort" | undefined; + type?: "major-snapshot" | "major" | "minor" | undefined; + filename?: string | undefined; + } - interface MessageEvent extends _MessageEvent {} - var MessageEvent: typeof globalThis extends { - onmessage: any; - MessageEvent: infer T; - } ? T - : typeof import("undici-types").MessageEvent; + /** An iterable iterator returned by the Node.js API. */ + // Default TReturn/TNext in v20 is `any`, for compatibility with the previously-used IterableIterator. + interface Iterator extends IteratorObject { + [Symbol.iterator](): NodeJS.Iterator; + } - interface EventSource extends _EventSource {} - var EventSource: typeof globalThis extends { onmessage: any; EventSource: infer T } ? T - : typeof import("undici-types").EventSource; - // #endregion fetch + /** An async iterable iterator returned by the Node.js API. */ + // Default TReturn/TNext in v20 is `any`, for compatibility with the previously-used AsyncIterableIterator. + interface AsyncIterator extends AsyncIteratorObject { + [Symbol.asyncIterator](): NodeJS.AsyncIterator; + } } diff --git a/types/node/v20/index.d.ts b/types/node/v20/index.d.ts index 0649eb54150dff..3c9eefcdfe24e4 100644 --- a/types/node/v20/index.d.ts +++ b/types/node/v20/index.d.ts @@ -36,6 +36,10 @@ // Definitions for Node.js modules that are not specific to any version of TypeScript: /// +/// +/// +/// +/// /// /// /// @@ -51,7 +55,6 @@ /// /// /// -/// /// /// /// diff --git a/types/node/v20/ts5.6/index.d.ts b/types/node/v20/ts5.6/index.d.ts index 886b8a8418c534..2fc2edfcf4f78c 100644 --- a/types/node/v20/ts5.6/index.d.ts +++ b/types/node/v20/ts5.6/index.d.ts @@ -36,6 +36,10 @@ // Definitions for Node.js modules that are not specific to any version of TypeScript: /// +/// +/// +/// +/// /// /// /// @@ -51,7 +55,6 @@ /// /// /// -/// /// /// /// diff --git a/types/node/v20/web-globals/abortcontroller.d.ts b/types/node/v20/web-globals/abortcontroller.d.ts new file mode 100644 index 00000000000000..f36cb9ae5d689f --- /dev/null +++ b/types/node/v20/web-globals/abortcontroller.d.ts @@ -0,0 +1,34 @@ +export {}; + +type _AbortController = typeof globalThis extends { onmessage: any } ? {} : AbortController; +interface AbortController { + readonly signal: AbortSignal; + abort(reason?: any): void; +} + +type _AbortSignal = typeof globalThis extends { onmessage: any } ? {} : AbortSignal; +interface AbortSignal extends EventTarget { + readonly aborted: boolean; + onabort: ((this: AbortSignal, ev: Event) => any) | null; + readonly reason: any; + throwIfAborted(): void; +} + +declare global { + interface AbortController extends _AbortController {} + var AbortController: typeof globalThis extends { onmessage: any; AbortController: infer T } ? T + : { + prototype: AbortController; + new(): AbortController; + }; + + interface AbortSignal extends _AbortSignal {} + var AbortSignal: typeof globalThis extends { onmessage: any; AbortSignal: infer T } ? T + : { + prototype: AbortSignal; + new(): AbortSignal; + abort(reason?: any): AbortSignal; + any(signals: AbortSignal[]): AbortSignal; + timeout(milliseconds: number): AbortSignal; + }; +} diff --git a/types/node/v20/web-globals/domexception.d.ts b/types/node/v20/web-globals/domexception.d.ts new file mode 100644 index 00000000000000..5b1662c4cf2ab9 --- /dev/null +++ b/types/node/v20/web-globals/domexception.d.ts @@ -0,0 +1,68 @@ +export {}; + +type _DOMException = typeof globalThis extends { onmessage: any } ? {} : DOMException; +interface DOMException extends Error { + readonly code: number; + readonly message: string; + readonly name: string; + readonly INDEX_SIZE_ERR: 1; + readonly DOMSTRING_SIZE_ERR: 2; + readonly HIERARCHY_REQUEST_ERR: 3; + readonly WRONG_DOCUMENT_ERR: 4; + readonly INVALID_CHARACTER_ERR: 5; + readonly NO_DATA_ALLOWED_ERR: 6; + readonly NO_MODIFICATION_ALLOWED_ERR: 7; + readonly NOT_FOUND_ERR: 8; + readonly NOT_SUPPORTED_ERR: 9; + readonly INUSE_ATTRIBUTE_ERR: 10; + readonly INVALID_STATE_ERR: 11; + readonly SYNTAX_ERR: 12; + readonly INVALID_MODIFICATION_ERR: 13; + readonly NAMESPACE_ERR: 14; + readonly INVALID_ACCESS_ERR: 15; + readonly VALIDATION_ERR: 16; + readonly TYPE_MISMATCH_ERR: 17; + readonly SECURITY_ERR: 18; + readonly NETWORK_ERR: 19; + readonly ABORT_ERR: 20; + readonly URL_MISMATCH_ERR: 21; + readonly QUOTA_EXCEEDED_ERR: 22; + readonly TIMEOUT_ERR: 23; + readonly INVALID_NODE_TYPE_ERR: 24; + readonly DATA_CLONE_ERR: 25; +} + +declare global { + interface DOMException extends _DOMException {} + var DOMException: typeof globalThis extends { onmessage: any; DOMException: infer T } ? T + : { + prototype: DOMException; + new(message?: string, name?: string): DOMException; + new(message?: string, options?: { name?: string; cause?: unknown }): DOMException; + readonly INDEX_SIZE_ERR: 1; + readonly DOMSTRING_SIZE_ERR: 2; + readonly HIERARCHY_REQUEST_ERR: 3; + readonly WRONG_DOCUMENT_ERR: 4; + readonly INVALID_CHARACTER_ERR: 5; + readonly NO_DATA_ALLOWED_ERR: 6; + readonly NO_MODIFICATION_ALLOWED_ERR: 7; + readonly NOT_FOUND_ERR: 8; + readonly NOT_SUPPORTED_ERR: 9; + readonly INUSE_ATTRIBUTE_ERR: 10; + readonly INVALID_STATE_ERR: 11; + readonly SYNTAX_ERR: 12; + readonly INVALID_MODIFICATION_ERR: 13; + readonly NAMESPACE_ERR: 14; + readonly INVALID_ACCESS_ERR: 15; + readonly VALIDATION_ERR: 16; + readonly TYPE_MISMATCH_ERR: 17; + readonly SECURITY_ERR: 18; + readonly NETWORK_ERR: 19; + readonly ABORT_ERR: 20; + readonly URL_MISMATCH_ERR: 21; + readonly QUOTA_EXCEEDED_ERR: 22; + readonly TIMEOUT_ERR: 23; + readonly INVALID_NODE_TYPE_ERR: 24; + readonly DATA_CLONE_ERR: 25; + }; +} diff --git a/types/node/dom-events.d.ts b/types/node/v20/web-globals/events.d.ts similarity index 75% rename from types/node/dom-events.d.ts rename to types/node/v20/web-globals/events.d.ts index cd6acde28196cf..a61e1faf9545b3 100644 --- a/types/node/dom-events.d.ts +++ b/types/node/v20/web-globals/events.d.ts @@ -1,37 +1,61 @@ -// Make this a module export {}; -// Conditional type aliases, which are later merged into the global scope. -// Will either be empty if the relevant web library is already present, or the @types/node definition otherwise. +interface AddEventListenerOptions extends EventListenerOptions { + once?: boolean; + passive?: boolean; + signal?: AbortSignal; +} -type __Event = typeof globalThis extends { onmessage: any } ? {} : Event; +type _CustomEvent = typeof globalThis extends { onmessage: any } ? {} : CustomEvent; +interface CustomEvent extends Event { + readonly detail: T; +} + +interface CustomEventInit extends EventInit { + detail?: T; +} + +type _Event = typeof globalThis extends { onmessage: any } ? {} : Event; interface Event { readonly bubbles: boolean; cancelBubble: boolean; readonly cancelable: boolean; readonly composed: boolean; - composedPath(): [EventTarget?]; readonly currentTarget: EventTarget | null; readonly defaultPrevented: boolean; readonly eventPhase: 0 | 2; - initEvent(type: string, bubbles?: boolean, cancelable?: boolean): void; readonly isTrusted: boolean; - preventDefault(): void; - readonly returnValue: boolean; + returnValue: boolean; readonly srcElement: EventTarget | null; - stopImmediatePropagation(): void; - stopPropagation(): void; readonly target: EventTarget | null; readonly timeStamp: number; readonly type: string; + composedPath(): [EventTarget?]; + initEvent(type: string, bubbles?: boolean, cancelable?: boolean): void; + preventDefault(): void; + stopImmediatePropagation(): void; + stopPropagation(): void; } -type __CustomEvent = typeof globalThis extends { onmessage: any } ? {} : CustomEvent; -interface CustomEvent extends Event { - readonly detail: T; +interface EventInit { + bubbles?: boolean; + cancelable?: boolean; + composed?: boolean; +} + +interface EventListener { + (evt: Event): void; +} + +interface EventListenerObject { + handleEvent(object: Event): void; +} + +interface EventListenerOptions { + capture?: boolean; } -type __EventTarget = typeof globalThis extends { onmessage: any } ? {} : EventTarget; +type _EventTarget = typeof globalThis extends { onmessage: any } ? {} : EventTarget; interface EventTarget { addEventListener( type: string, @@ -46,51 +70,22 @@ interface EventTarget { ): void; } -interface EventInit { - bubbles?: boolean; - cancelable?: boolean; - composed?: boolean; -} - -interface CustomEventInit extends EventInit { - detail?: T; -} - -interface EventListenerOptions { - capture?: boolean; -} - -interface AddEventListenerOptions extends EventListenerOptions { - once?: boolean; - passive?: boolean; - signal?: AbortSignal; -} - -interface EventListener { - (evt: Event): void; -} - -interface EventListenerObject { - handleEvent(object: Event): void; -} - -// Merge conditional interfaces into global scope, and conditionally declare global constructors. declare global { - interface Event extends __Event {} - var Event: typeof globalThis extends { onmessage: any; Event: infer T } ? T - : { - prototype: Event; - new(type: string, eventInitDict?: EventInit): Event; - }; - - interface CustomEvent extends __CustomEvent {} + interface CustomEvent extends _CustomEvent {} var CustomEvent: typeof globalThis extends { onmessage: any; CustomEvent: infer T } ? T : { prototype: CustomEvent; new(type: string, eventInitDict?: CustomEventInit): CustomEvent; }; - interface EventTarget extends __EventTarget {} + interface Event extends _Event {} + var Event: typeof globalThis extends { onmessage: any; Event: infer T } ? T + : { + prototype: Event; + new(type: string, eventInitDict?: EventInit): Event; + }; + + interface EventTarget extends _EventTarget {} var EventTarget: typeof globalThis extends { onmessage: any; EventTarget: infer T } ? T : { prototype: EventTarget; diff --git a/types/node/v20/web-globals/fetch.d.ts b/types/node/v20/web-globals/fetch.d.ts new file mode 100644 index 00000000000000..634d79eae939e5 --- /dev/null +++ b/types/node/v20/web-globals/fetch.d.ts @@ -0,0 +1,46 @@ +export {}; + +import * as undici from "undici-types"; + +type _EventSource = typeof globalThis extends { onmessage: any } ? {} : undici.EventSource; +type _FormData = typeof globalThis extends { onmessage: any } ? {} : undici.FormData; +type _Headers = typeof globalThis extends { onmessage: any } ? {} : undici.Headers; +type _MessageEvent = typeof globalThis extends { onmessage: any } ? {} : undici.MessageEvent; +type _Request = typeof globalThis extends { onmessage: any } ? {} : undici.Request; +type _RequestInit = typeof globalThis extends { onmessage: any } ? {} : undici.RequestInit; +type _Response = typeof globalThis extends { onmessage: any } ? {} : undici.Response; +type _ResponseInit = typeof globalThis extends { onmessage: any } ? {} : undici.ResponseInit; +type _WebSocket = typeof globalThis extends { onmessage: any } ? {} : undici.WebSocket; + +declare global { + function fetch( + input: string | URL | Request, + init?: RequestInit, + ): Promise; + + interface EventSource extends _EventSource {} + var EventSource: typeof globalThis extends { onmessage: any; EventSource: infer T } ? T : typeof undici.EventSource; + + interface FormData extends _FormData {} + var FormData: typeof globalThis extends { onmessage: any; FormData: infer T } ? T : typeof undici.FormData; + + interface Headers extends _Headers {} + var Headers: typeof globalThis extends { onmessage: any; Headers: infer T } ? T : typeof undici.Headers; + + interface MessageEvent extends _MessageEvent {} + var MessageEvent: typeof globalThis extends { onmessage: any; MessageEvent: infer T } ? T + : typeof undici.MessageEvent; + + interface Request extends _Request {} + var Request: typeof globalThis extends { onmessage: any; Request: infer T } ? T : typeof undici.Request; + + interface RequestInit extends _RequestInit {} + + interface Response extends _Response {} + var Response: typeof globalThis extends { onmessage: any; Response: infer T } ? T : typeof undici.Response; + + interface ResponseInit extends _ResponseInit {} + + interface WebSocket extends _WebSocket {} + var WebSocket: typeof globalThis extends { onmessage: any; WebSocket: infer T } ? T : typeof undici.WebSocket; +} diff --git a/types/node/v22/globals.d.ts b/types/node/v22/globals.d.ts index f9f22d7b754baf..dc5e84c0d25577 100644 --- a/types/node/v22/globals.d.ts +++ b/types/node/v22/globals.d.ts @@ -1,364 +1,170 @@ -export {}; // Make this a module +declare var global: typeof globalThis; -// #region Fetch and friends -// Conditional type aliases, used at the end of this file. -// 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; -type _Headers = typeof globalThis extends { onmessage: any } ? {} : import("undici-types").Headers; -type _MessageEvent = typeof globalThis extends { onmessage: any } ? {} : import("undici-types").MessageEvent; -type _RequestInit = typeof globalThis extends { onmessage: any } ? {} - : import("undici-types").RequestInit; -type _ResponseInit = typeof globalThis extends { onmessage: any } ? {} - : import("undici-types").ResponseInit; -type _WebSocket = typeof globalThis extends { onmessage: any } ? {} : import("undici-types").WebSocket; -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 } ? {} : { - readonly length: number; - clear(): void; - getItem(key: string): string | null; - key(index: number): string | null; - removeItem(key: string): void; - setItem(key: string, value: string): void; - [key: string]: any; -}; - -// #region DOMException -type _DOMException = typeof globalThis extends { onmessage: any } ? {} : NodeDOMException; -interface NodeDOMException extends Error { - readonly code: number; - readonly message: string; - readonly name: string; - readonly INDEX_SIZE_ERR: 1; - readonly DOMSTRING_SIZE_ERR: 2; - readonly HIERARCHY_REQUEST_ERR: 3; - readonly WRONG_DOCUMENT_ERR: 4; - readonly INVALID_CHARACTER_ERR: 5; - readonly NO_DATA_ALLOWED_ERR: 6; - readonly NO_MODIFICATION_ALLOWED_ERR: 7; - readonly NOT_FOUND_ERR: 8; - readonly NOT_SUPPORTED_ERR: 9; - readonly INUSE_ATTRIBUTE_ERR: 10; - readonly INVALID_STATE_ERR: 11; - readonly SYNTAX_ERR: 12; - readonly INVALID_MODIFICATION_ERR: 13; - readonly NAMESPACE_ERR: 14; - readonly INVALID_ACCESS_ERR: 15; - readonly VALIDATION_ERR: 16; - readonly TYPE_MISMATCH_ERR: 17; - readonly SECURITY_ERR: 18; - readonly NETWORK_ERR: 19; - readonly ABORT_ERR: 20; - readonly URL_MISMATCH_ERR: 21; - readonly QUOTA_EXCEEDED_ERR: 22; - readonly TIMEOUT_ERR: 23; - readonly INVALID_NODE_TYPE_ERR: 24; - readonly DATA_CLONE_ERR: 25; -} -interface NodeDOMExceptionConstructor { - prototype: DOMException; - new(message?: string, nameOrOptions?: string | { name?: string; cause?: unknown }): DOMException; - readonly INDEX_SIZE_ERR: 1; - readonly DOMSTRING_SIZE_ERR: 2; - readonly HIERARCHY_REQUEST_ERR: 3; - readonly WRONG_DOCUMENT_ERR: 4; - readonly INVALID_CHARACTER_ERR: 5; - readonly NO_DATA_ALLOWED_ERR: 6; - readonly NO_MODIFICATION_ALLOWED_ERR: 7; - readonly NOT_FOUND_ERR: 8; - readonly NOT_SUPPORTED_ERR: 9; - readonly INUSE_ATTRIBUTE_ERR: 10; - readonly INVALID_STATE_ERR: 11; - readonly SYNTAX_ERR: 12; - readonly INVALID_MODIFICATION_ERR: 13; - readonly NAMESPACE_ERR: 14; - readonly INVALID_ACCESS_ERR: 15; - readonly VALIDATION_ERR: 16; - readonly TYPE_MISMATCH_ERR: 17; - readonly SECURITY_ERR: 18; - readonly NETWORK_ERR: 19; - readonly ABORT_ERR: 20; - readonly URL_MISMATCH_ERR: 21; - readonly QUOTA_EXCEEDED_ERR: 22; - readonly TIMEOUT_ERR: 23; - readonly INVALID_NODE_TYPE_ERR: 24; - readonly DATA_CLONE_ERR: 25; -} -// #endregion DOMException - -declare global { - var global: typeof globalThis; - - var process: NodeJS.Process; - var console: Console; - - interface ErrorConstructor { - /** - * Creates a `.stack` property on `targetObject`, which when accessed returns - * a string representing the location in the code at which - * `Error.captureStackTrace()` was called. - * - * ```js - * const myObject = {}; - * Error.captureStackTrace(myObject); - * myObject.stack; // Similar to `new Error().stack` - * ``` - * - * The first line of the trace will be prefixed with - * `${myObject.name}: ${myObject.message}`. - * - * The optional `constructorOpt` argument accepts a function. If given, all frames - * above `constructorOpt`, including `constructorOpt`, will be omitted from the - * generated stack trace. - * - * The `constructorOpt` argument is useful for hiding implementation - * details of error generation from the user. For instance: - * - * ```js - * function a() { - * b(); - * } - * - * function b() { - * c(); - * } - * - * function c() { - * // Create an error without stack trace to avoid calculating the stack trace twice. - * const { stackTraceLimit } = Error; - * Error.stackTraceLimit = 0; - * const error = new Error(); - * Error.stackTraceLimit = stackTraceLimit; - * - * // Capture the stack trace above function b - * Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace - * throw error; - * } - * - * a(); - * ``` - */ - captureStackTrace(targetObject: object, constructorOpt?: Function): void; - /** - * @see https://v8.dev/docs/stack-trace-api#customizing-stack-traces - */ - prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any; - /** - * The `Error.stackTraceLimit` property specifies the number of stack frames - * collected by a stack trace (whether generated by `new Error().stack` or - * `Error.captureStackTrace(obj)`). - * - * The default value is `10` but may be set to any valid JavaScript number. Changes - * will affect any stack trace captured _after_ the value has been changed. - * - * If set to a non-number value, or set to a negative number, stack traces will - * not capture any frames. - */ - stackTraceLimit: number; - } +declare var process: NodeJS.Process; +declare var console: Console; +interface ErrorConstructor { /** - * Enable this API with the `--expose-gc` CLI flag. + * Creates a `.stack` property on `targetObject`, which when accessed returns + * a string representing the location in the code at which + * `Error.captureStackTrace()` was called. + * + * ```js + * const myObject = {}; + * Error.captureStackTrace(myObject); + * myObject.stack; // Similar to `new Error().stack` + * ``` + * + * The first line of the trace will be prefixed with + * `${myObject.name}: ${myObject.message}`. + * + * The optional `constructorOpt` argument accepts a function. If given, all frames + * above `constructorOpt`, including `constructorOpt`, will be omitted from the + * generated stack trace. + * + * The `constructorOpt` argument is useful for hiding implementation + * details of error generation from the user. For instance: + * + * ```js + * function a() { + * b(); + * } + * + * function b() { + * c(); + * } + * + * function c() { + * // Create an error without stack trace to avoid calculating the stack trace twice. + * const { stackTraceLimit } = Error; + * Error.stackTraceLimit = 0; + * const error = new Error(); + * Error.stackTraceLimit = stackTraceLimit; + * + * // Capture the stack trace above function b + * Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace + * throw error; + * } + * + * a(); + * ``` */ - var gc: NodeJS.GCFunction | undefined; - - namespace NodeJS { - interface CallSite { - getColumnNumber(): number | null; - getEnclosingColumnNumber(): number | null; - getEnclosingLineNumber(): number | null; - getEvalOrigin(): string | undefined; - getFileName(): string | null; - getFunction(): Function | undefined; - getFunctionName(): string | null; - getLineNumber(): number | null; - getMethodName(): string | null; - getPosition(): number; - getPromiseIndex(): number | null; - getScriptHash(): string; - getScriptNameOrSourceURL(): string | null; - getThis(): unknown; - getTypeName(): string | null; - isAsync(): boolean; - isConstructor(): boolean; - isEval(): boolean; - isNative(): boolean; - isPromiseAll(): boolean; - isToplevel(): boolean; - } - - interface ErrnoException extends Error { - errno?: number | undefined; - code?: string | undefined; - path?: string | undefined; - syscall?: string | undefined; - } - - interface ReadableStream extends EventEmitter { - readable: boolean; - read(size?: number): string | Buffer; - setEncoding(encoding: BufferEncoding): this; - pause(): this; - resume(): this; - isPaused(): boolean; - pipe(destination: T, options?: { end?: boolean | undefined }): T; - unpipe(destination?: WritableStream): this; - unshift(chunk: string | Uint8Array, encoding?: BufferEncoding): void; - wrap(oldStream: ReadableStream): this; - [Symbol.asyncIterator](): AsyncIterableIterator; - } - - interface WritableStream extends EventEmitter { - writable: boolean; - write(buffer: Uint8Array | string, cb?: (err?: Error | null) => void): boolean; - write(str: string, encoding?: BufferEncoding, cb?: (err?: Error | null) => void): boolean; - end(cb?: () => void): this; - end(data: string | Uint8Array, cb?: () => void): this; - end(str: string, encoding?: BufferEncoding, cb?: () => void): this; - } - - interface ReadWriteStream extends ReadableStream, WritableStream {} - - interface RefCounted { - ref(): this; - unref(): this; - } - - interface Dict { - [key: string]: T | undefined; - } - - interface ReadOnlyDict { - readonly [key: string]: T | undefined; - } - - interface GCFunction { - (minor?: boolean): void; - (options: NodeJS.GCOptions & { execution: "async" }): Promise; - (options: NodeJS.GCOptions): void; - } - - interface GCOptions { - execution?: "sync" | "async" | undefined; - flavor?: "regular" | "last-resort" | undefined; - type?: "major-snapshot" | "major" | "minor" | undefined; - filename?: string | undefined; - } - - /** An iterable iterator returned by the Node.js API. */ - // Default TReturn/TNext in v22 is `any`, for compatibility with the previously-used IterableIterator. - interface Iterator extends IteratorObject { - [Symbol.iterator](): NodeJS.Iterator; - } + captureStackTrace(targetObject: object, constructorOpt?: Function): void; + /** + * @see https://v8.dev/docs/stack-trace-api#customizing-stack-traces + */ + prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any; + /** + * The `Error.stackTraceLimit` property specifies the number of stack frames + * collected by a stack trace (whether generated by `new Error().stack` or + * `Error.captureStackTrace(obj)`). + * + * The default value is `10` but may be set to any valid JavaScript number. Changes + * will affect any stack trace captured _after_ the value has been changed. + * + * If set to a non-number value, or set to a negative number, stack traces will + * not capture any frames. + */ + stackTraceLimit: number; +} - /** An async iterable iterator returned by the Node.js API. */ - // Default TReturn/TNext in v22 is `any`, for compatibility with the previously-used AsyncIterableIterator. - interface AsyncIterator extends AsyncIteratorObject { - [Symbol.asyncIterator](): NodeJS.AsyncIterator; - } +/** + * Enable this API with the `--expose-gc` CLI flag. + */ +declare var gc: NodeJS.GCFunction | undefined; + +declare namespace NodeJS { + interface CallSite { + getColumnNumber(): number | null; + getEnclosingColumnNumber(): number | null; + getEnclosingLineNumber(): number | null; + getEvalOrigin(): string | undefined; + getFileName(): string | null; + getFunction(): Function | undefined; + getFunctionName(): string | null; + getLineNumber(): number | null; + getMethodName(): string | null; + getPosition(): number; + getPromiseIndex(): number | null; + getScriptHash(): string; + getScriptNameOrSourceURL(): string | null; + getThis(): unknown; + getTypeName(): string | null; + isAsync(): boolean; + isConstructor(): boolean; + isEval(): boolean; + isNative(): boolean; + isPromiseAll(): boolean; + isToplevel(): boolean; } - // Global DOM types - - interface DOMException extends _DOMException {} - var DOMException: typeof globalThis extends { onmessage: any; DOMException: infer T } ? T - : NodeDOMExceptionConstructor; - - // #region AbortController - interface AbortController { - readonly signal: AbortSignal; - abort(reason?: any): void; + interface ErrnoException extends Error { + errno?: number | undefined; + code?: string | undefined; + path?: string | undefined; + syscall?: string | undefined; } - var AbortController: typeof globalThis extends { onmessage: any; AbortController: infer T } ? T - : { - prototype: AbortController; - new(): AbortController; - }; - interface AbortSignal extends EventTarget { - readonly aborted: boolean; - onabort: ((this: AbortSignal, ev: Event) => any) | null; - readonly reason: any; - throwIfAborted(): void; + interface ReadableStream extends EventEmitter { + readable: boolean; + read(size?: number): string | Buffer; + setEncoding(encoding: BufferEncoding): this; + pause(): this; + resume(): this; + isPaused(): boolean; + pipe(destination: T, options?: { end?: boolean | undefined }): T; + unpipe(destination?: WritableStream): this; + unshift(chunk: string | Uint8Array, encoding?: BufferEncoding): void; + wrap(oldStream: ReadableStream): this; + [Symbol.asyncIterator](): AsyncIterableIterator; } - var AbortSignal: typeof globalThis extends { onmessage: any; AbortSignal: infer T } ? T - : { - prototype: AbortSignal; - new(): AbortSignal; - abort(reason?: any): AbortSignal; - any(signals: AbortSignal[]): AbortSignal; - timeout(milliseconds: number): AbortSignal; - }; - // #endregion AbortController - - // #region Storage - interface Storage extends _Storage {} - // 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; - }; - - var localStorage: Storage; - var sessionStorage: Storage; - // #endregion Storage - // #region fetch - interface RequestInit extends _RequestInit {} - - function fetch( - input: string | URL | globalThis.Request, - init?: RequestInit, - ): Promise; + interface WritableStream extends EventEmitter { + writable: boolean; + write(buffer: Uint8Array | string, cb?: (err?: Error | null) => void): boolean; + write(str: string, encoding?: BufferEncoding, cb?: (err?: Error | null) => void): boolean; + end(cb?: () => void): this; + end(data: string | Uint8Array, cb?: () => void): this; + end(str: string, encoding?: BufferEncoding, cb?: () => void): this; + } - interface Request extends _Request {} - var Request: typeof globalThis extends { - onmessage: any; - Request: infer T; - } ? T - : typeof import("undici-types").Request; + interface ReadWriteStream extends ReadableStream, WritableStream {} - interface ResponseInit extends _ResponseInit {} + interface RefCounted { + ref(): this; + unref(): this; + } - interface Response extends _Response {} - var Response: typeof globalThis extends { - onmessage: any; - Response: infer T; - } ? T - : typeof import("undici-types").Response; + interface Dict { + [key: string]: T | undefined; + } - interface FormData extends _FormData {} - var FormData: typeof globalThis extends { - onmessage: any; - FormData: infer T; - } ? T - : typeof import("undici-types").FormData; + interface ReadOnlyDict { + readonly [key: string]: T | undefined; + } - interface Headers extends _Headers {} - var Headers: typeof globalThis extends { - onmessage: any; - Headers: infer T; - } ? T - : typeof import("undici-types").Headers; + interface GCFunction { + (minor?: boolean): void; + (options: NodeJS.GCOptions & { execution: "async" }): Promise; + (options: NodeJS.GCOptions): void; + } - interface MessageEvent extends _MessageEvent {} - var MessageEvent: typeof globalThis extends { - onmessage: any; - MessageEvent: infer T; - } ? T - : typeof import("undici-types").MessageEvent; + interface GCOptions { + execution?: "sync" | "async" | undefined; + flavor?: "regular" | "last-resort" | undefined; + type?: "major-snapshot" | "major" | "minor" | undefined; + filename?: string | undefined; + } - interface WebSocket extends _WebSocket {} - var WebSocket: typeof globalThis extends { onmessage: any; WebSocket: infer T } ? T - : typeof import("undici-types").WebSocket; + /** An iterable iterator returned by the Node.js API. */ + // Default TReturn/TNext in v22 is `any`, for compatibility with the previously-used IterableIterator. + interface Iterator extends IteratorObject { + [Symbol.iterator](): NodeJS.Iterator; + } - interface EventSource extends _EventSource {} - var EventSource: typeof globalThis extends { onmessage: any; EventSource: infer T } ? T - : typeof import("undici-types").EventSource; - // #endregion fetch + /** An async iterable iterator returned by the Node.js API. */ + // Default TReturn/TNext in v22 is `any`, for compatibility with the previously-used AsyncIterableIterator. + interface AsyncIterator extends AsyncIteratorObject { + [Symbol.asyncIterator](): NodeJS.AsyncIterator; + } } diff --git a/types/node/v22/index.d.ts b/types/node/v22/index.d.ts index e99dc8388eb619..b6ee27303e6f61 100644 --- a/types/node/v22/index.d.ts +++ b/types/node/v22/index.d.ts @@ -36,6 +36,12 @@ // Definitions for Node.js modules that are not specific to any version of TypeScript: /// +/// +/// +/// +/// +/// +/// /// /// /// @@ -51,7 +57,6 @@ /// /// /// -/// /// /// /// diff --git a/types/node/v22/test/globals.ts b/types/node/v22/test/globals.ts index 629c58a443deae..8331a9d22c4ff9 100644 --- a/types/node/v22/test/globals.ts +++ b/types/node/v22/test/globals.ts @@ -90,6 +90,14 @@ declare var RANDOM_GLOBAL_VARIABLE: true; e.name; // $ExpectType string } +{ + navigator.hardwareConcurrency; // $ExpectType number + navigator.language; // $ExpectType string + navigator.languages; // $ExpectType readonly string[] + navigator.platform; // $ExpectType string + navigator.userAgent; // $ExpectType string; +} + { // @ts-expect-error The pseudoglobal `NodeJS` namespace should not be addressable outside ambient contexts NodeJS; diff --git a/types/node/v22/ts5.6/index.d.ts b/types/node/v22/ts5.6/index.d.ts index 96c9532a44e1c5..7265d7b705b530 100644 --- a/types/node/v22/ts5.6/index.d.ts +++ b/types/node/v22/ts5.6/index.d.ts @@ -36,6 +36,12 @@ // Definitions for Node.js modules that are not specific to any version of TypeScript: /// +/// +/// +/// +/// +/// +/// /// /// /// @@ -51,7 +57,6 @@ /// /// /// -/// /// /// /// diff --git a/types/node/v22/web-globals/abortcontroller.d.ts b/types/node/v22/web-globals/abortcontroller.d.ts new file mode 100644 index 00000000000000..f36cb9ae5d689f --- /dev/null +++ b/types/node/v22/web-globals/abortcontroller.d.ts @@ -0,0 +1,34 @@ +export {}; + +type _AbortController = typeof globalThis extends { onmessage: any } ? {} : AbortController; +interface AbortController { + readonly signal: AbortSignal; + abort(reason?: any): void; +} + +type _AbortSignal = typeof globalThis extends { onmessage: any } ? {} : AbortSignal; +interface AbortSignal extends EventTarget { + readonly aborted: boolean; + onabort: ((this: AbortSignal, ev: Event) => any) | null; + readonly reason: any; + throwIfAborted(): void; +} + +declare global { + interface AbortController extends _AbortController {} + var AbortController: typeof globalThis extends { onmessage: any; AbortController: infer T } ? T + : { + prototype: AbortController; + new(): AbortController; + }; + + interface AbortSignal extends _AbortSignal {} + var AbortSignal: typeof globalThis extends { onmessage: any; AbortSignal: infer T } ? T + : { + prototype: AbortSignal; + new(): AbortSignal; + abort(reason?: any): AbortSignal; + any(signals: AbortSignal[]): AbortSignal; + timeout(milliseconds: number): AbortSignal; + }; +} diff --git a/types/node/v22/web-globals/domexception.d.ts b/types/node/v22/web-globals/domexception.d.ts new file mode 100644 index 00000000000000..5b1662c4cf2ab9 --- /dev/null +++ b/types/node/v22/web-globals/domexception.d.ts @@ -0,0 +1,68 @@ +export {}; + +type _DOMException = typeof globalThis extends { onmessage: any } ? {} : DOMException; +interface DOMException extends Error { + readonly code: number; + readonly message: string; + readonly name: string; + readonly INDEX_SIZE_ERR: 1; + readonly DOMSTRING_SIZE_ERR: 2; + readonly HIERARCHY_REQUEST_ERR: 3; + readonly WRONG_DOCUMENT_ERR: 4; + readonly INVALID_CHARACTER_ERR: 5; + readonly NO_DATA_ALLOWED_ERR: 6; + readonly NO_MODIFICATION_ALLOWED_ERR: 7; + readonly NOT_FOUND_ERR: 8; + readonly NOT_SUPPORTED_ERR: 9; + readonly INUSE_ATTRIBUTE_ERR: 10; + readonly INVALID_STATE_ERR: 11; + readonly SYNTAX_ERR: 12; + readonly INVALID_MODIFICATION_ERR: 13; + readonly NAMESPACE_ERR: 14; + readonly INVALID_ACCESS_ERR: 15; + readonly VALIDATION_ERR: 16; + readonly TYPE_MISMATCH_ERR: 17; + readonly SECURITY_ERR: 18; + readonly NETWORK_ERR: 19; + readonly ABORT_ERR: 20; + readonly URL_MISMATCH_ERR: 21; + readonly QUOTA_EXCEEDED_ERR: 22; + readonly TIMEOUT_ERR: 23; + readonly INVALID_NODE_TYPE_ERR: 24; + readonly DATA_CLONE_ERR: 25; +} + +declare global { + interface DOMException extends _DOMException {} + var DOMException: typeof globalThis extends { onmessage: any; DOMException: infer T } ? T + : { + prototype: DOMException; + new(message?: string, name?: string): DOMException; + new(message?: string, options?: { name?: string; cause?: unknown }): DOMException; + readonly INDEX_SIZE_ERR: 1; + readonly DOMSTRING_SIZE_ERR: 2; + readonly HIERARCHY_REQUEST_ERR: 3; + readonly WRONG_DOCUMENT_ERR: 4; + readonly INVALID_CHARACTER_ERR: 5; + readonly NO_DATA_ALLOWED_ERR: 6; + readonly NO_MODIFICATION_ALLOWED_ERR: 7; + readonly NOT_FOUND_ERR: 8; + readonly NOT_SUPPORTED_ERR: 9; + readonly INUSE_ATTRIBUTE_ERR: 10; + readonly INVALID_STATE_ERR: 11; + readonly SYNTAX_ERR: 12; + readonly INVALID_MODIFICATION_ERR: 13; + readonly NAMESPACE_ERR: 14; + readonly INVALID_ACCESS_ERR: 15; + readonly VALIDATION_ERR: 16; + readonly TYPE_MISMATCH_ERR: 17; + readonly SECURITY_ERR: 18; + readonly NETWORK_ERR: 19; + readonly ABORT_ERR: 20; + readonly URL_MISMATCH_ERR: 21; + readonly QUOTA_EXCEEDED_ERR: 22; + readonly TIMEOUT_ERR: 23; + readonly INVALID_NODE_TYPE_ERR: 24; + readonly DATA_CLONE_ERR: 25; + }; +} diff --git a/types/node/v20/dom-events.d.ts b/types/node/v22/web-globals/events.d.ts similarity index 75% rename from types/node/v20/dom-events.d.ts rename to types/node/v22/web-globals/events.d.ts index cd6acde28196cf..a61e1faf9545b3 100644 --- a/types/node/v20/dom-events.d.ts +++ b/types/node/v22/web-globals/events.d.ts @@ -1,37 +1,61 @@ -// Make this a module export {}; -// Conditional type aliases, which are later merged into the global scope. -// Will either be empty if the relevant web library is already present, or the @types/node definition otherwise. +interface AddEventListenerOptions extends EventListenerOptions { + once?: boolean; + passive?: boolean; + signal?: AbortSignal; +} -type __Event = typeof globalThis extends { onmessage: any } ? {} : Event; +type _CustomEvent = typeof globalThis extends { onmessage: any } ? {} : CustomEvent; +interface CustomEvent extends Event { + readonly detail: T; +} + +interface CustomEventInit extends EventInit { + detail?: T; +} + +type _Event = typeof globalThis extends { onmessage: any } ? {} : Event; interface Event { readonly bubbles: boolean; cancelBubble: boolean; readonly cancelable: boolean; readonly composed: boolean; - composedPath(): [EventTarget?]; readonly currentTarget: EventTarget | null; readonly defaultPrevented: boolean; readonly eventPhase: 0 | 2; - initEvent(type: string, bubbles?: boolean, cancelable?: boolean): void; readonly isTrusted: boolean; - preventDefault(): void; - readonly returnValue: boolean; + returnValue: boolean; readonly srcElement: EventTarget | null; - stopImmediatePropagation(): void; - stopPropagation(): void; readonly target: EventTarget | null; readonly timeStamp: number; readonly type: string; + composedPath(): [EventTarget?]; + initEvent(type: string, bubbles?: boolean, cancelable?: boolean): void; + preventDefault(): void; + stopImmediatePropagation(): void; + stopPropagation(): void; } -type __CustomEvent = typeof globalThis extends { onmessage: any } ? {} : CustomEvent; -interface CustomEvent extends Event { - readonly detail: T; +interface EventInit { + bubbles?: boolean; + cancelable?: boolean; + composed?: boolean; +} + +interface EventListener { + (evt: Event): void; +} + +interface EventListenerObject { + handleEvent(object: Event): void; +} + +interface EventListenerOptions { + capture?: boolean; } -type __EventTarget = typeof globalThis extends { onmessage: any } ? {} : EventTarget; +type _EventTarget = typeof globalThis extends { onmessage: any } ? {} : EventTarget; interface EventTarget { addEventListener( type: string, @@ -46,51 +70,22 @@ interface EventTarget { ): void; } -interface EventInit { - bubbles?: boolean; - cancelable?: boolean; - composed?: boolean; -} - -interface CustomEventInit extends EventInit { - detail?: T; -} - -interface EventListenerOptions { - capture?: boolean; -} - -interface AddEventListenerOptions extends EventListenerOptions { - once?: boolean; - passive?: boolean; - signal?: AbortSignal; -} - -interface EventListener { - (evt: Event): void; -} - -interface EventListenerObject { - handleEvent(object: Event): void; -} - -// Merge conditional interfaces into global scope, and conditionally declare global constructors. declare global { - interface Event extends __Event {} - var Event: typeof globalThis extends { onmessage: any; Event: infer T } ? T - : { - prototype: Event; - new(type: string, eventInitDict?: EventInit): Event; - }; - - interface CustomEvent extends __CustomEvent {} + interface CustomEvent extends _CustomEvent {} var CustomEvent: typeof globalThis extends { onmessage: any; CustomEvent: infer T } ? T : { prototype: CustomEvent; new(type: string, eventInitDict?: CustomEventInit): CustomEvent; }; - interface EventTarget extends __EventTarget {} + interface Event extends _Event {} + var Event: typeof globalThis extends { onmessage: any; Event: infer T } ? T + : { + prototype: Event; + new(type: string, eventInitDict?: EventInit): Event; + }; + + interface EventTarget extends _EventTarget {} var EventTarget: typeof globalThis extends { onmessage: any; EventTarget: infer T } ? T : { prototype: EventTarget; diff --git a/types/node/v22/web-globals/fetch.d.ts b/types/node/v22/web-globals/fetch.d.ts new file mode 100644 index 00000000000000..634d79eae939e5 --- /dev/null +++ b/types/node/v22/web-globals/fetch.d.ts @@ -0,0 +1,46 @@ +export {}; + +import * as undici from "undici-types"; + +type _EventSource = typeof globalThis extends { onmessage: any } ? {} : undici.EventSource; +type _FormData = typeof globalThis extends { onmessage: any } ? {} : undici.FormData; +type _Headers = typeof globalThis extends { onmessage: any } ? {} : undici.Headers; +type _MessageEvent = typeof globalThis extends { onmessage: any } ? {} : undici.MessageEvent; +type _Request = typeof globalThis extends { onmessage: any } ? {} : undici.Request; +type _RequestInit = typeof globalThis extends { onmessage: any } ? {} : undici.RequestInit; +type _Response = typeof globalThis extends { onmessage: any } ? {} : undici.Response; +type _ResponseInit = typeof globalThis extends { onmessage: any } ? {} : undici.ResponseInit; +type _WebSocket = typeof globalThis extends { onmessage: any } ? {} : undici.WebSocket; + +declare global { + function fetch( + input: string | URL | Request, + init?: RequestInit, + ): Promise; + + interface EventSource extends _EventSource {} + var EventSource: typeof globalThis extends { onmessage: any; EventSource: infer T } ? T : typeof undici.EventSource; + + interface FormData extends _FormData {} + var FormData: typeof globalThis extends { onmessage: any; FormData: infer T } ? T : typeof undici.FormData; + + interface Headers extends _Headers {} + var Headers: typeof globalThis extends { onmessage: any; Headers: infer T } ? T : typeof undici.Headers; + + interface MessageEvent extends _MessageEvent {} + var MessageEvent: typeof globalThis extends { onmessage: any; MessageEvent: infer T } ? T + : typeof undici.MessageEvent; + + interface Request extends _Request {} + var Request: typeof globalThis extends { onmessage: any; Request: infer T } ? T : typeof undici.Request; + + interface RequestInit extends _RequestInit {} + + interface Response extends _Response {} + var Response: typeof globalThis extends { onmessage: any; Response: infer T } ? T : typeof undici.Response; + + interface ResponseInit extends _ResponseInit {} + + interface WebSocket extends _WebSocket {} + var WebSocket: typeof globalThis extends { onmessage: any; WebSocket: infer T } ? T : typeof undici.WebSocket; +} diff --git a/types/node/v22/web-globals/navigator.d.ts b/types/node/v22/web-globals/navigator.d.ts new file mode 100644 index 00000000000000..e1798afcaa50cc --- /dev/null +++ b/types/node/v22/web-globals/navigator.d.ts @@ -0,0 +1,22 @@ +export {}; + +// lib.webworker has `WorkerNavigator` rather than `Navigator`, so conditionals use `onabort` instead of `onmessage` +type _Navigator = typeof globalThis extends { onabort: any } ? {} : Navigator; +interface Navigator { + readonly hardwareConcurrency: number; + readonly language: string; + readonly languages: readonly string[]; + readonly platform: string; + readonly userAgent: string; +} + +declare global { + interface Navigator extends _Navigator {} + var Navigator: typeof globalThis extends { onabort: any; Navigator: infer T } ? T : { + prototype: Navigator; + new(): Navigator; + }; + + // Needs conditional inference for lib.dom and lib.webworker compatibility + var navigator: typeof globalThis extends { onmessage: any; navigator: infer T } ? T : Navigator; +} diff --git a/types/node/v22/web-globals/storage.d.ts b/types/node/v22/web-globals/storage.d.ts new file mode 100644 index 00000000000000..fd61cfc83bd818 --- /dev/null +++ b/types/node/v22/web-globals/storage.d.ts @@ -0,0 +1,24 @@ +export {}; + +// These interfaces are absent from lib.webworker, so the conditionals use `onabort` rather than `onmessage` +type _Storage = typeof globalThis extends { onabort: any } ? {} : Storage; +interface Storage { + readonly length: number; + clear(): void; + getItem(key: string): string | null; + key(index: number): string | null; + removeItem(key: string): void; + setItem(key: string, value: string): void; + [key: string]: any; +} + +declare global { + interface Storage extends _Storage {} + var Storage: typeof globalThis extends { onabort: any; Storage: infer T } ? T : { + prototype: Storage; + new(): Storage; + }; + + var localStorage: Storage; + var sessionStorage: Storage; +} diff --git a/types/node/web-globals/abortcontroller.d.ts b/types/node/web-globals/abortcontroller.d.ts new file mode 100644 index 00000000000000..f36cb9ae5d689f --- /dev/null +++ b/types/node/web-globals/abortcontroller.d.ts @@ -0,0 +1,34 @@ +export {}; + +type _AbortController = typeof globalThis extends { onmessage: any } ? {} : AbortController; +interface AbortController { + readonly signal: AbortSignal; + abort(reason?: any): void; +} + +type _AbortSignal = typeof globalThis extends { onmessage: any } ? {} : AbortSignal; +interface AbortSignal extends EventTarget { + readonly aborted: boolean; + onabort: ((this: AbortSignal, ev: Event) => any) | null; + readonly reason: any; + throwIfAborted(): void; +} + +declare global { + interface AbortController extends _AbortController {} + var AbortController: typeof globalThis extends { onmessage: any; AbortController: infer T } ? T + : { + prototype: AbortController; + new(): AbortController; + }; + + interface AbortSignal extends _AbortSignal {} + var AbortSignal: typeof globalThis extends { onmessage: any; AbortSignal: infer T } ? T + : { + prototype: AbortSignal; + new(): AbortSignal; + abort(reason?: any): AbortSignal; + any(signals: AbortSignal[]): AbortSignal; + timeout(milliseconds: number): AbortSignal; + }; +} diff --git a/types/node/web-globals/domexception.d.ts b/types/node/web-globals/domexception.d.ts new file mode 100644 index 00000000000000..5b1662c4cf2ab9 --- /dev/null +++ b/types/node/web-globals/domexception.d.ts @@ -0,0 +1,68 @@ +export {}; + +type _DOMException = typeof globalThis extends { onmessage: any } ? {} : DOMException; +interface DOMException extends Error { + readonly code: number; + readonly message: string; + readonly name: string; + readonly INDEX_SIZE_ERR: 1; + readonly DOMSTRING_SIZE_ERR: 2; + readonly HIERARCHY_REQUEST_ERR: 3; + readonly WRONG_DOCUMENT_ERR: 4; + readonly INVALID_CHARACTER_ERR: 5; + readonly NO_DATA_ALLOWED_ERR: 6; + readonly NO_MODIFICATION_ALLOWED_ERR: 7; + readonly NOT_FOUND_ERR: 8; + readonly NOT_SUPPORTED_ERR: 9; + readonly INUSE_ATTRIBUTE_ERR: 10; + readonly INVALID_STATE_ERR: 11; + readonly SYNTAX_ERR: 12; + readonly INVALID_MODIFICATION_ERR: 13; + readonly NAMESPACE_ERR: 14; + readonly INVALID_ACCESS_ERR: 15; + readonly VALIDATION_ERR: 16; + readonly TYPE_MISMATCH_ERR: 17; + readonly SECURITY_ERR: 18; + readonly NETWORK_ERR: 19; + readonly ABORT_ERR: 20; + readonly URL_MISMATCH_ERR: 21; + readonly QUOTA_EXCEEDED_ERR: 22; + readonly TIMEOUT_ERR: 23; + readonly INVALID_NODE_TYPE_ERR: 24; + readonly DATA_CLONE_ERR: 25; +} + +declare global { + interface DOMException extends _DOMException {} + var DOMException: typeof globalThis extends { onmessage: any; DOMException: infer T } ? T + : { + prototype: DOMException; + new(message?: string, name?: string): DOMException; + new(message?: string, options?: { name?: string; cause?: unknown }): DOMException; + readonly INDEX_SIZE_ERR: 1; + readonly DOMSTRING_SIZE_ERR: 2; + readonly HIERARCHY_REQUEST_ERR: 3; + readonly WRONG_DOCUMENT_ERR: 4; + readonly INVALID_CHARACTER_ERR: 5; + readonly NO_DATA_ALLOWED_ERR: 6; + readonly NO_MODIFICATION_ALLOWED_ERR: 7; + readonly NOT_FOUND_ERR: 8; + readonly NOT_SUPPORTED_ERR: 9; + readonly INUSE_ATTRIBUTE_ERR: 10; + readonly INVALID_STATE_ERR: 11; + readonly SYNTAX_ERR: 12; + readonly INVALID_MODIFICATION_ERR: 13; + readonly NAMESPACE_ERR: 14; + readonly INVALID_ACCESS_ERR: 15; + readonly VALIDATION_ERR: 16; + readonly TYPE_MISMATCH_ERR: 17; + readonly SECURITY_ERR: 18; + readonly NETWORK_ERR: 19; + readonly ABORT_ERR: 20; + readonly URL_MISMATCH_ERR: 21; + readonly QUOTA_EXCEEDED_ERR: 22; + readonly TIMEOUT_ERR: 23; + readonly INVALID_NODE_TYPE_ERR: 24; + readonly DATA_CLONE_ERR: 25; + }; +} diff --git a/types/node/v22/dom-events.d.ts b/types/node/web-globals/events.d.ts similarity index 75% rename from types/node/v22/dom-events.d.ts rename to types/node/web-globals/events.d.ts index cd6acde28196cf..a61e1faf9545b3 100644 --- a/types/node/v22/dom-events.d.ts +++ b/types/node/web-globals/events.d.ts @@ -1,37 +1,61 @@ -// Make this a module export {}; -// Conditional type aliases, which are later merged into the global scope. -// Will either be empty if the relevant web library is already present, or the @types/node definition otherwise. +interface AddEventListenerOptions extends EventListenerOptions { + once?: boolean; + passive?: boolean; + signal?: AbortSignal; +} -type __Event = typeof globalThis extends { onmessage: any } ? {} : Event; +type _CustomEvent = typeof globalThis extends { onmessage: any } ? {} : CustomEvent; +interface CustomEvent extends Event { + readonly detail: T; +} + +interface CustomEventInit extends EventInit { + detail?: T; +} + +type _Event = typeof globalThis extends { onmessage: any } ? {} : Event; interface Event { readonly bubbles: boolean; cancelBubble: boolean; readonly cancelable: boolean; readonly composed: boolean; - composedPath(): [EventTarget?]; readonly currentTarget: EventTarget | null; readonly defaultPrevented: boolean; readonly eventPhase: 0 | 2; - initEvent(type: string, bubbles?: boolean, cancelable?: boolean): void; readonly isTrusted: boolean; - preventDefault(): void; - readonly returnValue: boolean; + returnValue: boolean; readonly srcElement: EventTarget | null; - stopImmediatePropagation(): void; - stopPropagation(): void; readonly target: EventTarget | null; readonly timeStamp: number; readonly type: string; + composedPath(): [EventTarget?]; + initEvent(type: string, bubbles?: boolean, cancelable?: boolean): void; + preventDefault(): void; + stopImmediatePropagation(): void; + stopPropagation(): void; } -type __CustomEvent = typeof globalThis extends { onmessage: any } ? {} : CustomEvent; -interface CustomEvent extends Event { - readonly detail: T; +interface EventInit { + bubbles?: boolean; + cancelable?: boolean; + composed?: boolean; +} + +interface EventListener { + (evt: Event): void; +} + +interface EventListenerObject { + handleEvent(object: Event): void; +} + +interface EventListenerOptions { + capture?: boolean; } -type __EventTarget = typeof globalThis extends { onmessage: any } ? {} : EventTarget; +type _EventTarget = typeof globalThis extends { onmessage: any } ? {} : EventTarget; interface EventTarget { addEventListener( type: string, @@ -46,51 +70,22 @@ interface EventTarget { ): void; } -interface EventInit { - bubbles?: boolean; - cancelable?: boolean; - composed?: boolean; -} - -interface CustomEventInit extends EventInit { - detail?: T; -} - -interface EventListenerOptions { - capture?: boolean; -} - -interface AddEventListenerOptions extends EventListenerOptions { - once?: boolean; - passive?: boolean; - signal?: AbortSignal; -} - -interface EventListener { - (evt: Event): void; -} - -interface EventListenerObject { - handleEvent(object: Event): void; -} - -// Merge conditional interfaces into global scope, and conditionally declare global constructors. declare global { - interface Event extends __Event {} - var Event: typeof globalThis extends { onmessage: any; Event: infer T } ? T - : { - prototype: Event; - new(type: string, eventInitDict?: EventInit): Event; - }; - - interface CustomEvent extends __CustomEvent {} + interface CustomEvent extends _CustomEvent {} var CustomEvent: typeof globalThis extends { onmessage: any; CustomEvent: infer T } ? T : { prototype: CustomEvent; new(type: string, eventInitDict?: CustomEventInit): CustomEvent; }; - interface EventTarget extends __EventTarget {} + interface Event extends _Event {} + var Event: typeof globalThis extends { onmessage: any; Event: infer T } ? T + : { + prototype: Event; + new(type: string, eventInitDict?: EventInit): Event; + }; + + interface EventTarget extends _EventTarget {} var EventTarget: typeof globalThis extends { onmessage: any; EventTarget: infer T } ? T : { prototype: EventTarget; diff --git a/types/node/web-globals/fetch.d.ts b/types/node/web-globals/fetch.d.ts new file mode 100644 index 00000000000000..995cd723d7a65c --- /dev/null +++ b/types/node/web-globals/fetch.d.ts @@ -0,0 +1,50 @@ +export {}; + +import * as undici from "undici-types"; + +type _CloseEvent = typeof globalThis extends { onmessage: any } ? {} : undici.CloseEvent; +type _EventSource = typeof globalThis extends { onmessage: any } ? {} : undici.EventSource; +type _FormData = typeof globalThis extends { onmessage: any } ? {} : undici.FormData; +type _Headers = typeof globalThis extends { onmessage: any } ? {} : undici.Headers; +type _MessageEvent = typeof globalThis extends { onmessage: any } ? {} : undici.MessageEvent; +type _Request = typeof globalThis extends { onmessage: any } ? {} : undici.Request; +type _RequestInit = typeof globalThis extends { onmessage: any } ? {} : undici.RequestInit; +type _Response = typeof globalThis extends { onmessage: any } ? {} : undici.Response; +type _ResponseInit = typeof globalThis extends { onmessage: any } ? {} : undici.ResponseInit; +type _WebSocket = typeof globalThis extends { onmessage: any } ? {} : undici.WebSocket; + +declare global { + function fetch( + input: string | URL | Request, + init?: RequestInit, + ): Promise; + + interface CloseEvent extends _CloseEvent {} + var CloseEvent: typeof globalThis extends { onmessage: any; CloseEvent: infer T } ? T : typeof undici.CloseEvent; + + interface EventSource extends _EventSource {} + var EventSource: typeof globalThis extends { onmessage: any; EventSource: infer T } ? T : typeof undici.EventSource; + + interface FormData extends _FormData {} + var FormData: typeof globalThis extends { onmessage: any; FormData: infer T } ? T : typeof undici.FormData; + + interface Headers extends _Headers {} + var Headers: typeof globalThis extends { onmessage: any; Headers: infer T } ? T : typeof undici.Headers; + + interface MessageEvent extends _MessageEvent {} + var MessageEvent: typeof globalThis extends { onmessage: any; MessageEvent: infer T } ? T + : typeof undici.MessageEvent; + + interface Request extends _Request {} + var Request: typeof globalThis extends { onmessage: any; Request: infer T } ? T : typeof undici.Request; + + interface RequestInit extends _RequestInit {} + + interface Response extends _Response {} + var Response: typeof globalThis extends { onmessage: any; Response: infer T } ? T : typeof undici.Response; + + interface ResponseInit extends _ResponseInit {} + + interface WebSocket extends _WebSocket {} + var WebSocket: typeof globalThis extends { onmessage: any; WebSocket: infer T } ? T : typeof undici.WebSocket; +} diff --git a/types/node/web-globals/navigator.d.ts b/types/node/web-globals/navigator.d.ts new file mode 100644 index 00000000000000..e1798afcaa50cc --- /dev/null +++ b/types/node/web-globals/navigator.d.ts @@ -0,0 +1,22 @@ +export {}; + +// lib.webworker has `WorkerNavigator` rather than `Navigator`, so conditionals use `onabort` instead of `onmessage` +type _Navigator = typeof globalThis extends { onabort: any } ? {} : Navigator; +interface Navigator { + readonly hardwareConcurrency: number; + readonly language: string; + readonly languages: readonly string[]; + readonly platform: string; + readonly userAgent: string; +} + +declare global { + interface Navigator extends _Navigator {} + var Navigator: typeof globalThis extends { onabort: any; Navigator: infer T } ? T : { + prototype: Navigator; + new(): Navigator; + }; + + // Needs conditional inference for lib.dom and lib.webworker compatibility + var navigator: typeof globalThis extends { onmessage: any; navigator: infer T } ? T : Navigator; +} diff --git a/types/node/web-globals/storage.d.ts b/types/node/web-globals/storage.d.ts new file mode 100644 index 00000000000000..fd61cfc83bd818 --- /dev/null +++ b/types/node/web-globals/storage.d.ts @@ -0,0 +1,24 @@ +export {}; + +// These interfaces are absent from lib.webworker, so the conditionals use `onabort` rather than `onmessage` +type _Storage = typeof globalThis extends { onabort: any } ? {} : Storage; +interface Storage { + readonly length: number; + clear(): void; + getItem(key: string): string | null; + key(index: number): string | null; + removeItem(key: string): void; + setItem(key: string, value: string): void; + [key: string]: any; +} + +declare global { + interface Storage extends _Storage {} + var Storage: typeof globalThis extends { onabort: any; Storage: infer T } ? T : { + prototype: Storage; + new(): Storage; + }; + + var localStorage: Storage; + var sessionStorage: Storage; +}