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

TypeScript error: "Type 'AbortSignal' is missing the following properties from type 'AbortSignal': reason, throwIfAborted" #36

Open
OliverJAsh opened this issue Jun 14, 2022 · 5 comments

Comments

@OliverJAsh
Copy link

Using latest version of all packages at time of writing:

package.json:

{
  "dependencies": {
    "@types/node": "^17.0.42",
    "abort-controller": "^3.0.0",
    "typescript": "^4.7.3"
  }
}

main.ts:

import AbortController from "abort-controller";

/*
Type 'typeof AbortController' is not assignable to type '{ new (): AbortController; prototype: AbortController; }'.
  The types of 'prototype.signal' are incompatible between these types.
    Type 'AbortSignal' is missing the following properties from type 'AbortSignal': reason, throwIfAborted
*/
globalThis.AbortController = AbortController;

We can fix it by using the global AbortController type:

diff --git a/node_modules/abort-controller/dist/abort-controller.d.ts b/node_modules/abort-controller/dist/abort-controller.d.ts
index 75852fb..aa25471 100644
--- a/node_modules/abort-controller/dist/abort-controller.d.ts
+++ b/node_modules/abort-controller/dist/abort-controller.d.ts
@@ -24,20 +24,6 @@ declare class AbortSignal extends EventTarget<Events, EventAttributes> {
  * The AbortController.
  * @see https://dom.spec.whatwg.org/#abortcontroller
  */
-declare class AbortController {
-    /**
-     * Initialize this controller.
-     */
-    constructor()
-    /**
-     * Returns the `AbortSignal` object associated with this object.
-     */
-    readonly signal: AbortSignal
-    /**
-     * Abort and signal to any observers that the associated activity is to be aborted.
-     */
-    abort(): void
-}
-
+declare const AbortController: typeof globalThis.AbortController;
 export default AbortController
 export { AbortController, AbortSignal }
@crowmagnumb
Copy link

Yup, I simply deleted my line

import AbortController from "abort-controller";

and then removed that npm package

npm un abort-controller

and I was good to go.

@riboher
Copy link

riboher commented Oct 25, 2022

Yup, I simply deleted my line

import AbortController from "abort-controller";

and then removed that npm package

npm un abort-controller

and I was good to go.

I might be wrong but this solution would only work if the code was to be executed only on the browser, right? What if we need to use it in a node service? Is this interface globally available as well in this case?

@crowmagnumb
Copy link

This was in a node service for me using axios. I don't know what provides the type but I do have @types/node installed so maybe it comes from there? Or maybe @types/express which I also have installed?

@cmd-johnson
Copy link

Since v15.4.0 Node.js comes with AbortController out of the box, so that's probably why it still works even after removing this package.

@crowmagnumb
Copy link

@cmd-johnson cheers, I realize that's why it works, I was mentioning the types for VSCode to not complain, which undoubtedly is provided by my @types/node install. I think I incorrectly ascertained that @riboher was missing that. Seems they were just speculating.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants