Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node: Add missing typings for AbortSignal in node #61412

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 80 additions & 0 deletions types/node/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,31 @@ interface AbortController {
abort(): void;
}

interface AbortSignalEventMap {
"abort": NodeJS.Event;
}

/** A signal object that allows you to communicate with a DOM request (such as a Fetch) and abort it if required via an AbortController object. */
interface AbortSignal {
/**
* Returns true if this AbortSignal's AbortController has signaled to abort, and false otherwise.
*/
readonly aborted: boolean;
onabort: ((this: AbortSignal, ev: NodeJS.Event) => any) | null;
/**
* An optional reason specified when the AbortSignal was triggered.
* @since v17.2.0
*/
readonly reason: any;
/**
* If {@link aborted} is true, throws {@link reason}.
* @since v17.3.0
*/
throwIfAborted(): void;
addEventListener<K extends keyof AbortSignalEventMap>(type: K, listener: (this: AbortSignal, ev: AbortSignalEventMap[K]) => any, options?: boolean | NodeJS.AddEventListenerOptions): void;
addEventListener(type: string, listener: NodeJS.EventListenerOrEventListenerObject, options?: boolean | NodeJS.AddEventListenerOptions): void;
removeEventListener<K extends keyof AbortSignalEventMap>(type: K, listener: (this: AbortSignal, ev: AbortSignalEventMap[K]) => any, options?: boolean | NodeJS.EventListenerOptions): void;
removeEventListener(type: string, listener: NodeJS.EventListenerOrEventListenerObject, options?: boolean | NodeJS.EventListenerOptions): void;
}

declare var AbortController: {
Expand Down Expand Up @@ -291,4 +310,65 @@ declare namespace NodeJS {
interface ReadOnlyDict<T> {
readonly [key: string]: T | undefined;
}
/** An event which takes place in the DOM. */
interface Event {
/** Returns true or false depending on how event was initialized. True if event goes through its target's ancestors in reverse tree order, and false otherwise. */
readonly bubbles: boolean;
cancelBubble: boolean;
/** Returns true or false depending on how event was initialized. Its return value does not always carry meaning, but true can indicate that part of the operation during which event was dispatched, can be canceled by invoking the preventDefault() method. */
readonly cancelable: boolean;
/** Returns true or false depending on how event was initialized. True if event invokes listeners past a ShadowRoot node that is the root of its target, and false otherwise. */
readonly composed: boolean;
/** Returns the object whose event listener's callback is currently being invoked. */
readonly currentTarget: EventTarget | null;
/** Returns true if preventDefault() was invoked successfully to indicate cancelation, and false otherwise. */
readonly defaultPrevented: boolean;
/** Returns the event's phase, which is one of NONE, CAPTURING_PHASE, AT_TARGET, and BUBBLING_PHASE. */
readonly eventPhase: number;
/** Returns true if event was dispatched by the user agent, and false otherwise. */
readonly isTrusted: boolean;
/** @deprecated */
returnValue: boolean;
/** @deprecated */
readonly srcElement: EventTarget | null;
/** Returns the object to which event is dispatched (its target). */
readonly target: EventTarget | null;
/** Returns the event's timestamp as the number of milliseconds measured relative to the time origin. */
readonly timeStamp: DOMHighResTimeStamp;
/** Returns the type of event, e.g. "click", "hashchange", or "submit". */
readonly type: string;
/** Returns the invocation target objects of event's path (objects on which listeners will be invoked), except for any nodes in shadow trees of which the shadow root's mode is "closed" that are not reachable from event's currentTarget. */
composedPath(): EventTarget[];
/** @deprecated */
initEvent(type: string, bubbles?: boolean, cancelable?: boolean): void;
/** If invoked when the cancelable attribute value is true, and while executing a listener for the event with passive set to false, signals to the operation that caused event to be dispatched that it needs to be canceled. */
preventDefault(): void;
/** Invoking this method prevents event from reaching any registered event listeners after the current one finishes running and, when dispatched in a tree, also prevents event from reaching any other objects. */
stopImmediatePropagation(): void;
/** When dispatched in a tree, invoking this method prevents event from reaching any objects other than the current object. */
stopPropagation(): void;
readonly AT_TARGET: number;
readonly BUBBLING_PHASE: number;
readonly CAPTURING_PHASE: number;
readonly NONE: number;
}
interface EventListener {
(evt: Event): void;
}

interface EventListenerObject {
handleEvent(object: Event): void;
}

type EventListenerOrEventListenerObject = EventListener | EventListenerObject;

interface EventListenerOptions {
capture?: boolean;
}

interface AddEventListenerOptions extends EventListenerOptions {
once?: boolean;
passive?: boolean;
signal?: AbortSignal;
}
}
13 changes: 13 additions & 0 deletions types/node/test/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,16 @@ declare var RANDOM_GLOBAL_VARIABLE: true;
const arrayBuffer = new ArrayBuffer(0);
structuredClone({ test: arrayBuffer }, { transfer: [arrayBuffer] }); // $ExpectType { test: ArrayBuffer; }
}

// AbortController & AbortSignal
{
const controller = new AbortController();
let bool: boolean;
bool = controller.signal.aborted;
const listener = (event: Event) => {};
controller.signal.addEventListener("abort", listener);
controller.signal.removeEventListener("abort", listener);
controller.signal.onabort = listener;
controller.signal.throwIfAborted();
controller.abort();
}
71 changes: 71 additions & 0 deletions types/node/v14/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ interface AbortController {
abort(): void;
}

interface AbortSignalEventMap {
"abort": NodeJS.Event;
}

/**
* A signal object that allows you to communicate with a DOM request (such as a Fetch) and abort it if required via an AbortController object.
* @since v14.7.0
Expand All @@ -104,6 +108,11 @@ interface AbortSignal {
* @since v14.7.0
*/
readonly aborted: boolean;
onabort: ((this: AbortSignal, ev: NodeJS.Event) => any) | null;
addEventListener<K extends keyof AbortSignalEventMap>(type: K, listener: (this: AbortSignal, ev: AbortSignalEventMap[K]) => any, options?: boolean | NodeJS.AddEventListenerOptions): void;
addEventListener(type: string, listener: NodeJS.EventListenerOrEventListenerObject, options?: boolean | NodeJS.AddEventListenerOptions): void;
removeEventListener<K extends keyof AbortSignalEventMap>(type: K, listener: (this: AbortSignal, ev: AbortSignalEventMap[K]) => any, options?: boolean | NodeJS.EventListenerOptions): void;
removeEventListener(type: string, listener: NodeJS.EventListenerOrEventListenerObject, options?: boolean | NodeJS.EventListenerOptions): void;
}

declare var AbortController: {
Expand Down Expand Up @@ -743,4 +752,66 @@ declare namespace NodeJS {
interface ReadOnlyDict<T> {
readonly [key: string]: T | undefined;
}

/** An event which takes place in the DOM. */
interface Event {
/** Returns true or false depending on how event was initialized. True if event goes through its target's ancestors in reverse tree order, and false otherwise. */
readonly bubbles: boolean;
cancelBubble: boolean;
/** Returns true or false depending on how event was initialized. Its return value does not always carry meaning, but true can indicate that part of the operation during which event was dispatched, can be canceled by invoking the preventDefault() method. */
readonly cancelable: boolean;
/** Returns true or false depending on how event was initialized. True if event invokes listeners past a ShadowRoot node that is the root of its target, and false otherwise. */
readonly composed: boolean;
/** Returns the object whose event listener's callback is currently being invoked. */
readonly currentTarget: EventTarget | null;
/** Returns true if preventDefault() was invoked successfully to indicate cancelation, and false otherwise. */
readonly defaultPrevented: boolean;
/** Returns the event's phase, which is one of NONE, CAPTURING_PHASE, AT_TARGET, and BUBBLING_PHASE. */
readonly eventPhase: number;
/** Returns true if event was dispatched by the user agent, and false otherwise. */
readonly isTrusted: boolean;
/** @deprecated */
returnValue: boolean;
/** @deprecated */
readonly srcElement: EventTarget | null;
/** Returns the object to which event is dispatched (its target). */
readonly target: EventTarget | null;
/** Returns the event's timestamp as the number of milliseconds measured relative to the time origin. */
readonly timeStamp: DOMHighResTimeStamp;
/** Returns the type of event, e.g. "click", "hashchange", or "submit". */
readonly type: string;
/** Returns the invocation target objects of event's path (objects on which listeners will be invoked), except for any nodes in shadow trees of which the shadow root's mode is "closed" that are not reachable from event's currentTarget. */
composedPath(): EventTarget[];
/** @deprecated */
initEvent(type: string, bubbles?: boolean, cancelable?: boolean): void;
/** If invoked when the cancelable attribute value is true, and while executing a listener for the event with passive set to false, signals to the operation that caused event to be dispatched that it needs to be canceled. */
preventDefault(): void;
/** Invoking this method prevents event from reaching any registered event listeners after the current one finishes running and, when dispatched in a tree, also prevents event from reaching any other objects. */
stopImmediatePropagation(): void;
/** When dispatched in a tree, invoking this method prevents event from reaching any objects other than the current object. */
stopPropagation(): void;
readonly AT_TARGET: number;
readonly BUBBLING_PHASE: number;
readonly CAPTURING_PHASE: number;
readonly NONE: number;
}

interface EventListener {
(evt: Event): void;
}

interface EventListenerObject {
handleEvent(object: Event): void;
}

type EventListenerOrEventListenerObject = EventListener | EventListenerObject;

interface EventListenerOptions {
capture?: boolean;
}

interface AddEventListenerOptions extends EventListenerOptions {
once?: boolean;
passive?: boolean;
}
}
12 changes: 12 additions & 0 deletions types/node/v14/test/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,15 @@ declare var RANDOM_GLOBAL_VARIABLE: true;

const abortController = new global.AbortController();
abortController.signal; // $ExpectType AbortSignal

// AbortController & AbortSignal
{
const controller = new AbortController();
let bool: boolean;
bool = controller.signal.aborted;
const listener = (event: Event) => {};
controller.signal.addEventListener("abort", listener);
controller.signal.removeEventListener("abort", listener);
controller.signal.onabort = listener;
controller.abort();
}
76 changes: 76 additions & 0 deletions types/node/v16/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,26 @@ interface AbortController {
abort(): void;
}

interface AbortSignalEventMap {
"abort": NodeJS.Event;
}

/** A signal object that allows you to communicate with a DOM request (such as a Fetch) and abort it if required via an AbortController object. */
interface AbortSignal {
/**
* Returns true if this AbortSignal's AbortController has signaled to abort, and false otherwise.
*/
readonly aborted: boolean;
onabort: ((this: AbortSignal, ev: NodeJS.Event) => any) | null;
/**
* An optional reason specified when the AbortSignal was triggered.
* @since v16.14.0
*/
readonly reason: any;
addEventListener<K extends keyof AbortSignalEventMap>(type: K, listener: (this: AbortSignal, ev: AbortSignalEventMap[K]) => any, options?: boolean | NodeJS.AddEventListenerOptions): void;
addEventListener(type: string, listener: NodeJS.EventListenerOrEventListenerObject, options?: boolean | NodeJS.AddEventListenerOptions): void;
removeEventListener<K extends keyof AbortSignalEventMap>(type: K, listener: (this: AbortSignal, ev: AbortSignalEventMap[K]) => any, options?: boolean | NodeJS.EventListenerOptions): void;
removeEventListener(type: string, listener: NodeJS.EventListenerOrEventListenerObject, options?: boolean | NodeJS.EventListenerOptions): void;
}

declare var AbortController: {
Expand Down Expand Up @@ -281,4 +295,66 @@ declare namespace NodeJS {
interface ReadOnlyDict<T> {
readonly [key: string]: T | undefined;
}

/** An event which takes place in the DOM. */
interface Event {
/** Returns true or false depending on how event was initialized. True if event goes through its target's ancestors in reverse tree order, and false otherwise. */
readonly bubbles: boolean;
cancelBubble: boolean;
/** Returns true or false depending on how event was initialized. Its return value does not always carry meaning, but true can indicate that part of the operation during which event was dispatched, can be canceled by invoking the preventDefault() method. */
readonly cancelable: boolean;
/** Returns true or false depending on how event was initialized. True if event invokes listeners past a ShadowRoot node that is the root of its target, and false otherwise. */
readonly composed: boolean;
/** Returns the object whose event listener's callback is currently being invoked. */
readonly currentTarget: EventTarget | null;
/** Returns true if preventDefault() was invoked successfully to indicate cancelation, and false otherwise. */
readonly defaultPrevented: boolean;
/** Returns the event's phase, which is one of NONE, CAPTURING_PHASE, AT_TARGET, and BUBBLING_PHASE. */
readonly eventPhase: number;
/** Returns true if event was dispatched by the user agent, and false otherwise. */
readonly isTrusted: boolean;
/** @deprecated */
returnValue: boolean;
/** @deprecated */
readonly srcElement: EventTarget | null;
/** Returns the object to which event is dispatched (its target). */
readonly target: EventTarget | null;
/** Returns the event's timestamp as the number of milliseconds measured relative to the time origin. */
readonly timeStamp: DOMHighResTimeStamp;
/** Returns the type of event, e.g. "click", "hashchange", or "submit". */
readonly type: string;
/** Returns the invocation target objects of event's path (objects on which listeners will be invoked), except for any nodes in shadow trees of which the shadow root's mode is "closed" that are not reachable from event's currentTarget. */
composedPath(): EventTarget[];
/** @deprecated */
initEvent(type: string, bubbles?: boolean, cancelable?: boolean): void;
/** If invoked when the cancelable attribute value is true, and while executing a listener for the event with passive set to false, signals to the operation that caused event to be dispatched that it needs to be canceled. */
preventDefault(): void;
/** Invoking this method prevents event from reaching any registered event listeners after the current one finishes running and, when dispatched in a tree, also prevents event from reaching any other objects. */
stopImmediatePropagation(): void;
/** When dispatched in a tree, invoking this method prevents event from reaching any objects other than the current object. */
stopPropagation(): void;
readonly AT_TARGET: number;
readonly BUBBLING_PHASE: number;
readonly CAPTURING_PHASE: number;
readonly NONE: number;
}

interface EventListener {
(evt: Event): void;
}

interface EventListenerObject {
handleEvent(object: Event): void;
}

type EventListenerOrEventListenerObject = EventListener | EventListenerObject;

interface EventListenerOptions {
capture?: boolean;
}

interface AddEventListenerOptions extends EventListenerOptions {
once?: boolean;
passive?: boolean;
}
}
13 changes: 13 additions & 0 deletions types/node/v16/test/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,16 @@ declare var RANDOM_GLOBAL_VARIABLE: true;
gc();
}
}

// AbortController & AbortSignal
{
const controller = new AbortController();
let bool: boolean;
bool = controller.signal.aborted;
controller.signal.reason;
const listener = (event: Event) => {};
controller.signal.addEventListener("abort", listener);
controller.signal.removeEventListener("abort", listener);
controller.signal.onabort = listener;
controller.abort();
}