Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 712
Alexander Mills Avatar asked Jun 11 '17 01:06

Alexander Mills


Video Answer


1 Answers

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.

like image 147
Behrooz Avatar answered Oct 17 '22 12:10

Behrooz