我有以下TypeScript代码:
const allDescribeBlocks: Array<ITestSuite> = suman.allDescribeBlocks;
async.eachSeries(allDescribeBlocks, function (block: ITestSuite, cb: Function) {
//....
}, cb);
这将伴随着警告:
ITestSuite []类型的参数不能分配给类型的参数 字典&LT; {}取代。 ITestSuite []中缺少索引签名。
如何解决?
答案 0 :(得分: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.json
将定义目录添加到"typeRoots": ["node_modules/@types"]
,它应该可以解决问题。
修改 - 此外,您应该避免对简单类型进行Array<T>
定义,而是使用T[]
。