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

1

6

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

Sublunary answered 11/6, 2017 at 1:45 Comment(1)
From the name Dictionary, it seems to suggest it is expecting an object, not an array.Zomba
D
2

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.

Dissociation answered 8/3, 2018 at 2:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.