6

我有以下打字稿代码:

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

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

//....

}, cb);

这将转换为警告:

ITestSuite[] 类型的参数不可分配给 Dictionary<{}> 类型的参数。ITestSuite[] 中缺少索引签名。

怎么修?

这是确切的警告:在此处输入图像描述

4

1 回答 1

2

您是否为异步库安装了最新的类型定义?

npm install --save @types/async

我刚刚检查了他们的来源,它应该接受数组和集合:

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;

我认为如果您安装类型定义模块并将定义目录添加到您的tsconfig.jsonusing 中"typeRoots": ["node_modules/@types"],它应该可以解决问题。

编辑 - 此外,您应该避免Array<T>定义简单类型并T[]改为使用。

于 2018-03-08T02:00:34.287 回答