mysticatea / abort-controller

An implementation of WHATWG AbortController interface.
MIT License
302 stars 36 forks source link

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

Open OliverJAsh opened 2 years ago

OliverJAsh commented 2 years ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

@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.