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 declarations are failing on iterables #47

Open
slavafomin opened this issue Oct 6, 2017 · 6 comments
Open

TypeScript declarations are failing on iterables #47

slavafomin opened this issue Oct 6, 2017 · 6 comments

Comments

@slavafomin
Copy link

Hello!

Thank you for this great library!

However, when I try to use iterable with it, the compilers throws errors:

let i = 1;

const generator = function * () {
  while (true) {
    yield new Promise(resolve => {
      const j = i++;
      console.log('Starting to process #' + j);
      setTimeout(() => {
        console.log('Finished processing #' + j);
        resolve();
      }, 1000);
    });
  }
};

const pool = new PromisePool(
  generator(),
  // Argument of type 'IterableIterator<Promise<{}>>' is not assignable to parameter of
  // type '() => void | PromiseLike<{}>'. Type 'IterableIterator<Promise<{}>>' provides
  // no match for the signature '(): void | PromiseLike<{}>'.
  10
);

pool.start().then(() => console.log('Complete'));

So I have to call it this way in order to suppress the error: new PromisePool(generator() as any, 10);.

Maybe, the declaration should be changed to:

source: () => PromiseLike<A> | IterableIterator<Promise<A>> ?

Thanks!

@slavafomin
Copy link
Author

Also, the PromiseLike<A> looks incorrect to me, shouldn't it be () => PromiseLike<A> | null?

@timdp
Copy link
Owner

timdp commented Oct 7, 2017

Thanks for the feedback! There's another issue around the typings that I haven't been able to get to yet. I didn't write them and I don't know that much about TypeScript, but I've been meaning to get into it. I'll also accept a PR with fixes though!

@slavafomin
Copy link
Author

I will try to look into it if time permits. By the way, what other issue you were having with declarations?

@vhfmag
Copy link

vhfmag commented Dec 5, 2017

I just ran into the same problem and fixed it using patch-package, so I could easily submit a PR. What do you think?

@aappddeevv
Copy link

aappddeevv commented Jan 8, 2018

I had issues with the typings. I wrote up:

// fix 2018-01-08

export = PromisePool

declare class PromisePool<A> extends EventTarget {
  // skip GeneratorFunction as that is documented to be deprecated starting v3
  constructor(
    source: IterableIterator<Promise<A>> | Promise<A> | (() => (Promise<A> | void)) | A,
    concurrency: number,
    options?: PromisePool.Options<A>
  )

  concurrency(concurrency: number): number
  size(): number
  active(): boolean
  promise(): PromiseLike<void>
  start(): PromiseLike<void>
}

declare namespace PromisePool {
  export interface Options<A> {
    promise?: Promise<A>
  }
}

Added the new index.d.ts to typings/es6-promise-pool then added a reference to the import using tsconfig: "paths": { "es6-promise-pool": ["./typings/es6-promise-pool"]} then imported via import * as PromisePool from "es6-promise-pool" or you could use import PromisePool = require("es6-promise-pool").

But I've not extensively testing the typings though. I needed the result of calling a GeneratorFunction and the others look valid based on reviewing the source code. Note that I just overrode the typings. I use webpack for final bundling.

@aMarCruz
Copy link

This definitions also fails with error TS2304: Cannot find name 'EventTarget'

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

5 participants