Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 类型数组中缺少索引签名<;字符串>;用打字稿_Typescript_Typescript2.0 - Fatal编程技术网

Typescript 类型数组中缺少索引签名<;字符串>;用打字稿

Typescript 类型数组中缺少索引签名<;字符串>;用打字稿,typescript,typescript2.0,Typescript,Typescript2.0,我在TypeScript中看到此错误: let fn = function (transformPaths: Array<string>, cb: Function) { async.mapLimit(transformPaths, 5, function (t: string, cb: Function){ // problem...does not compile }, cb); } 代码本身如下所示: let fn = functio

我在TypeScript中看到此错误:

 let fn = function (transformPaths: Array<string>, cb: Function) {

    async.mapLimit(transformPaths, 5, function (t: string, cb: Function){

     // problem...does not compile

    }, cb);
 } 

代码本身如下所示:

let fn =  function (transformPaths: Array<string>, cb: Function) {

    async.mapLimit(transformPaths, 5, function (t: string, $cb: Function) {

         // ....

    }, cb);

};
设fn=function(transformpath:Array,cb:function){
mapLimit(transformpath,5,函数(t:string,$cb:function){
// ....
},cb);
};
错误消息是:

TS2345:类型为“string[]”的参数不能分配给的参数 键入“Dictionary”。类型“string[]”中缺少索引签名

我怎样才能纠正这个问题?从屏幕截图中可以看到,async库中的键入不喜欢将字符串的普通数组作为第一个参数传递给async.mapLimit,但是为什么呢

我99%确定我需要向字符串数组添加索引签名,但我该怎么做?

下面是一些“编译”的东西,但我不知道它是否正确(这似乎对任何人都没有帮助):

导出接口ISumanTransformPaths扩展数组{
[索引:字符串]:对象
}
现在,当我使用
(transformpath:isumantransformpath)
时,它会编译,但我不确定这是否正确。

它们是:

mapLimit(arr:T[]| IterableIterator,limit:number,iterator:AsyncResultIterator,callback?:asyncResultaryCallback):void;
mapLimit(arr:Dictionary,limit:number,迭代器:AsyncResultIterator,callback?:AsyncResultaryCallback):void;
因此,它可以与
字典
或数组/
IterableIterator


我不知道为什么编译器会推断第二个签名而不是第一个,但可能是因为您传递的
回调应该是第四个参数,而第三个参数需要是迭代器。

感谢Nitzan确认
@types/async
可能是正确的。这就解决了问题:

之前:

 let fn = function (transformPaths: Array<string>, cb: Function) {

    async.mapLimit(transformPaths, 5, function (t: string, cb: Function){

     // problem...does not compile

    }, cb);
 } 
设fn=function(transformpath:Array,cb:function){
mapLimit(转换路径,5,函数(t:string,cb:function){
//问题…无法编译
},cb);
} 
之后:(仔细看,只有一个变化)

let fn=function(transformpath:Array,cb:AsyncResultarray回调){
mapLimit(转换路径,5,函数(t:string,cb:function){
//现在它编译了!
},cb);
};

您需要另一种类型。你有一个数组,你需要一个看起来像js对象的东西(例如:
{key1:“value1”,key2:“value2”}
)@NitzanTomer我不这么认为-我认为异步库会接受一个Iterable,它可以是一个对象或数组。但我同意,这不是很清楚。我不知道这个
async
库,但通常
Dictionary
表示键/值。你能为它共享一个指向定义文件的链接吗?是的,我同意你的观点,我99%确定字典的意思是键/值。实际上,我现在坚信async的def类型是不正确的。它应该是可编辑的,而不是字典。谢谢Nitzan,我弄明白了,我添加了一个答案,说明什么对我有用。我仍然在寻找这个该死问题的答案:github.com/DefinitelyTyped/DefinitelyTyped/issues/24469
 let fn = function (transformPaths: Array<string>, cb: Function) {

    async.mapLimit(transformPaths, 5, function (t: string, cb: Function){

     // problem...does not compile

    }, cb);
 } 
  let fn = function (transformPaths: Array<string>, cb: AsyncResultArrayCallback<Error,Array<any>>) {

        async.mapLimit(transformPaths, 5, function (t: string, cb: Function) {

        // now it compiles!

        }, cb);

   };