arraysnode.jstypescriptasync.jstypescript2.2

Pass array to async library eachSeries - expects 'Dictionary<{}>'


I have the following TypeScript code:

const allDescribeBlocks: Array<ITestSuite> = suman.allDescribeBlocks;

async.eachSeries(allDescribeBlocks, function (block: ITestSuite, cb: Function) {

//....

}, cb);

this will transpile with warnings:

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

How to fix?

Here is the exact warning:enter image description here


Solution

  • Have you installed the latest type definition for async library?

    npm install --save @types/async
    

    I just checked their source and it should accept both array and collection:

    export function each<T, E>(arr: T[] | IterableIterator<T>, iterator: AsyncIterator<T, E>, callback?: ErrorCallback<E>): void;
    export function each<T, E>(arr: Dictionary<T>, iterator: AsyncIterator<T, E>, callback?: ErrorCallback<E>): void;
    export const eachSeries: typeof each;
    

    I think if you install the type definition module and add the definition directory to your tsconfig.json using "typeRoots": ["node_modules/@types"], it should solve the issue.

    Edit - Also, you should avoid Array<T> definition for simple types and use T[] instead.