Skip to content

Commit 67073d0

Browse files
Extract AbortSignal type from globalThis
1 parent c2f7590 commit 67073d0

File tree

3 files changed

+13
-47
lines changed

3 files changed

+13
-47
lines changed

etc/web-streams-polyfill.api.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
```ts
66

77
// @public
8-
export interface AbortSignal {
9-
readonly aborted: boolean;
10-
addEventListener(type: 'abort', listener: () => void): void;
11-
readonly reason?: any;
12-
removeEventListener(type: 'abort', listener: () => void): void;
13-
}
8+
export type AbortSignal = typeof globalThis extends {
9+
AbortSignal: {
10+
prototype: infer T;
11+
};
12+
} ? T : never;
1413

1514
// @public
1615
export class ByteLengthQueuingStrategy implements QueuingStrategy<ArrayBufferView> {

src/lib/abort-signal.ts

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,11 @@
33
* via its associated `AbortController` object.
44
*
55
* @remarks
6-
* This interface is compatible with the `AbortSignal` interface defined in TypeScript's DOM types.
7-
* It is redefined here, so it can be polyfilled without a DOM, for example with
8-
* {@link https://www.npmjs.com/package/abortcontroller-polyfill | abortcontroller-polyfill} in a Node environment.
6+
* This is equivalent to the `AbortSignal` interface defined in TypeScript's DOM types or `@types/node`.
97
*
108
* @public
119
*/
12-
export interface AbortSignal {
13-
/**
14-
* Whether the request is aborted.
15-
*/
16-
readonly aborted: boolean;
17-
18-
/**
19-
* If aborted, returns the reason for aborting.
20-
*/
21-
readonly reason?: any;
22-
23-
/**
24-
* Add an event listener to be triggered when this signal becomes aborted.
25-
*/
26-
addEventListener(type: 'abort', listener: () => void): void;
27-
28-
/**
29-
* Remove an event listener that was previously added with {@link AbortSignal.addEventListener}.
30-
*/
31-
removeEventListener(type: 'abort', listener: () => void): void;
32-
}
10+
export type AbortSignal = typeof globalThis extends { AbortSignal: { prototype: infer T } } ? T : never;
3311

3412
export function isAbortSignal(value: unknown): value is AbortSignal {
3513
if (typeof value !== 'object' || value === null) {
@@ -47,32 +25,22 @@ export function isAbortSignal(value: unknown): value is AbortSignal {
4725
* A controller object that allows you to abort an `AbortSignal` when desired.
4826
*
4927
* @remarks
50-
* This interface is compatible with the `AbortController` interface defined in TypeScript's DOM types.
51-
* It is redefined here, so it can be polyfilled without a DOM, for example with
52-
* {@link https://www.npmjs.com/package/abortcontroller-polyfill | abortcontroller-polyfill} in a Node environment.
28+
* This is equivalent to the `AbortController` interface defined in TypeScript's DOM types or `@types/node`.
5329
*
5430
* @internal
5531
*/
56-
export interface AbortController {
57-
readonly signal: AbortSignal;
58-
59-
abort(reason?: any): void;
60-
}
61-
62-
interface AbortControllerConstructor {
63-
new(): AbortController;
64-
}
65-
66-
const supportsAbortController = typeof (AbortController as any) === 'function';
32+
// Trick with globalThis inspired by @types/node
33+
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/0c370ead967cb97b1758d8fa15d09011fb3f58ea/types/node/globals.d.ts#L226
34+
export type AbortController = typeof globalThis extends { AbortController: { prototype: infer T } } ? T : never;
6735

6836
/**
6937
* Construct a new AbortController, if supported by the platform.
7038
*
7139
* @internal
7240
*/
7341
export function createAbortController(): AbortController | undefined {
74-
if (supportsAbortController) {
75-
return new (AbortController as AbortControllerConstructor)();
42+
if (typeof AbortController === 'function') {
43+
return new AbortController();
7644
}
7745
return undefined;
7846
}

test/types/writable-stream.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ const abortPromise: Promise<void> = writableStream.abort('aborted');
4141

4242
// Compatibility with stream types from DOM
4343
// FIXME Remove deprecated WritableStreamDefaultController.abortReason
44-
// FIXME Align our AbortSignal definition with TypeScript's version
4544
// const domUnderlyingSink: UnderlyingSink<string> = underlyingSink;
4645
const domWritableStream: WritableStream<string> = writableStream;
4746
// const domController: WritableStreamDefaultController = controller;

0 commit comments

Comments
 (0)