2

I am seeing this error with TypeScript:

enter image description here

The code itself just looks like this:

let fn =  function (transformPaths: Array<string>, cb: Function) {

    async.mapLimit(transformPaths, 5, function (t: string, $cb: Function) {

         // ....

    }, cb);

};

The error message is:

TS2345: Argument of type 'string[]' is not assignable to parameter of type 'Dictionary<{}>'. Index signature is missing in type 'string[]'.

How can I correct this? As you can see from the screenshot, the typings from the async library do not like a plain array of strings being passed as the first argument to async.mapLimit, but why?

I am 99% certain I need to add an index signature to the array of strings, but how do I do that?

Here is something that "compiles" but I don't know if it's correct (this doesn't seem to be helping anyone):

export interface ISumanTransformPaths extends Array<string> {
  [index:string]: Object
}

now when I use (transformPaths: ISumanTransformPaths), then it compiles, but I am not sure if this correct.

6
  • You need a different type. You have an array, and you need what seems to be a js object (i.e.: { key1: "value1", key2: "value2" }) Commented Jun 27, 2017 at 18:59
  • @NitzanTomer I don't think so - I think async library will accept an Iterable, which can be either an Object or Array. But I agree, it's not very clear. Commented Jun 27, 2017 at 19:00
  • I don't know this async library, but usually Dictionary means key/value. Can you share a link to the definition file for it? Commented Jun 27, 2017 at 19:03
  • Yes I agree with you, I am 99% certain that Dictionary means key/value. I actually now believe firmly that the type def for async is incorrect. It should be Iterable<T> not Dictionary<{}>. Commented Jun 27, 2017 at 19:09
  • npmjs.com/package/@types/async Commented Jun 27, 2017 at 19:09

2 Answers 2

3

The signatures for this method are:

mapLimit<T, R, E>(arr: T[] | IterableIterator<T>, limit: number, iterator: AsyncResultIterator<T, R, E>, callback?: AsyncResultArrayCallback<R, E>): void;
mapLimit<T, R, E>(arr: Dictionary<T>, limit: number, iterator: AsyncResultIterator<T, R, E>, callback?: AsyncResultArrayCallback<R, E>): void;

So it can work with a Dictionary or an array/IterableIterator.
I'm not sure why the compiler infers the 2nd signature instead of the first, but maybe because the callback you pass it should be the 4th argument, while the 3rd needs to be an iterator.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks Nitzan, I figured it out, I added an answer showing what works for me.
I am still looking for answer to this damn problem: github.com/DefinitelyTyped/DefinitelyTyped/issues/24469
0

Thanks Nitzan for confirming that @types/async is probably correct. This solved the problem:

before:

 let fn = function (transformPaths: Array<string>, cb: Function) {

    async.mapLimit(transformPaths, 5, function (t: string, cb: Function){

     // problem...does not compile

    }, cb);
 } 

after: (look carefully, there is just one change)

  let fn = function (transformPaths: Array<string>, cb: AsyncResultArrayCallback<Error,Array<any>>) {

        async.mapLimit(transformPaths, 5, function (t: string, cb: Function) {

        // now it compiles!

        }, cb);

   };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.