mo / abortcontroller-polyfill

Polyfill for the AbortController DOM API and abortable fetch (stub that calls catch, doesn't actually abort request).
MIT License
325 stars 23 forks source link

Typescript module definitions #44

Open lukasolson opened 4 years ago

lukasolson commented 4 years ago

I'm trying to use this in a Typescript project, but am having to in an odd way because this library doesn't have type module definitions. Any plans to add this? I'm happy to spin up a PR if it's helpful.

mo commented 4 years ago

I don't think anyone has created TS definitions yet, a PR would be great

mo commented 4 years ago

What approach are you planning; rewrite in TS and generate .d.ts files on build, or manually writing the .d.ts files?

mo commented 4 years ago

FWIW, I pushed the commit "e9880c8" to master now, it upgrades all dependencies to the latest versions and re-pins chromedriver/geckodriver to fresh versions. To run the tests you must have chrome v80 installed and then just do "npm test". The firefox tests are a bit more flexible in terms on version, you can run them using "npm run test-firefox".

xamgore commented 3 years ago

I do believe the following typings can be used:

declare module 'abortcontroller-polyfill/dist/cjs-ponyfill' {
  /** A controller object that allows you to abort one or more DOM requests as and when desired. */
  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(): void
  }

  const AbortController: {
    prototype: AbortController
    new(): AbortController
  }

  interface AbortSignalEventMap {
    'abort': 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 extends EventTarget {
    /**
     * Returns true if this AbortSignal's AbortController has signaled to abort, and false otherwise.
     */
    readonly aborted: boolean
    onabort: ((this: AbortSignal, ev: Event) => any) | null

    addEventListener<K extends keyof AbortSignalEventMap>(type: K, listener: (this: AbortSignal, ev: AbortSignalEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void

    addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void

    removeEventListener<K extends keyof AbortSignalEventMap>(type: K, listener: (this: AbortSignal, ev: AbortSignalEventMap[K]) => any, options?: boolean | EventListenerOptions): void

    removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void
  }

  const AbortSignal: {
    prototype: AbortSignal
    new(): AbortSignal
  }
}
ghost commented 1 year ago

@mo Could you add the above code to .d.ts file please?

TechQuery commented 2 months ago

TypeScript has fixed this issue in latest version: microsoft/TypeScript#51567