Skip to content

AbortController.abort is missing parameter support #47505

Closed

Description

@laurenzhonauer

lib Update Request

Configuration Check

My compilation target is es5 and my lib is ["dom", "dom.iterable", "esnext"]. (But i think that does not matter here)

Missing / Incorrect Definition

Abort Controller.abort() in lib.dom.d.ts does not accept a reason parameter.

Abort Controller MDN Description mentions that this reason is already supported by browsers.

It would be quite handy to have this in place because it would be possible to set a timeout to api calls (just one example) and then get a custom error object thrown in case the request takes too long.

The Error Parameter bellow controller.abort(new Error("This call takes too long")); would not work with the current implementation. Instead its only possible to abort like controller.abort() and then get a "standard" abort error thrown.

Sample Code

const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(new Error("This call takes too long"), 10000);

const request = await fetch(`www.some.api`, {
    signal: controller.signal,
  });

  clearTimeout(timeoutId);

Documentation Link

https://developer.mozilla.org/en-US/docs/Web/API/AbortController/abort

Same Issue in [@types/node]

Activity

  1. added
    Bug
    Domain: lib.d.ts
    on Jan 20, 2022
  2. johannpinson commented on May 11, 2022

    @johannpinson

    Hello laurenzhonauer

    For your information, it looks to be fixed with the 4.6 release (feb 2022).

    The latest v4.6.4 has the reason parameter
    https://github.com/microsoft/TypeScript/blob/v4.6.4/lib/lib.dom.d.ts#L1895-L1906

  3. douglascayers commented on May 11, 2022

    @douglascayers

    Thanks Johann Pinson (@johannpinson) -- although I'm on that version of typescript, in my project the type inference is coming from @types/node which doesn't yet have the reason parameter.

    Edit:
    In the meantime, I think I can get around it via:

    import 'node';
    
    /**
     * Workaround due to outdated typings.
     * https://github.com/microsoft/TypeScript/issues/47505
     */
    declare global {
        interface AbortController {
            /**
             * Returns the AbortSignal object associated with this object.
             */
            readonly signal: AbortSignal;
            /**
             * Invoking this method will set this object's AbortSignal's aborted flag
             * and signal to any observers that the associated activity is to be aborted.
             */
            abort(reason?: any): void;
        }
    
        interface AbortSignal {
            /**
             * Returns true if this AbortSignal's AbortController has signaled to abort, and false otherwise.
             */
            readonly aborted: boolean;
            /**
             * Returns the reason, if any, when this AbortSignal's AbortController signaled to abort.
             */
            readonly reason?: any;
        }
    }
  4. douglascayers commented on May 12, 2022

    @douglascayers

    Although typescript v4.6.4 updated the AbortController.abort method to accept a reason parameter, the AbortSignal interface was not updated to expose the reason property =/

    https://github.com/microsoft/TypeScript/blob/v4.6.4/lib/lib.dom.d.ts#L1913-L1921

  5. tonivj5 commented on Mar 3, 2023

    @tonivj5

    I think this issue is resolved, isn't it?

  6. RyanCavanaugh commented on Mar 3, 2023

    @RyanCavanaugh
    Member

    Seems so. Thanks!

  7. eden-eitani commented on Apr 16, 2023

    @eden-eitani

    Doug Ayers (@douglascayers) did you find another solution?

  8. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDomain: lib.d.ts

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions