2016-05-31 70 views
0

我剛開始使用Angular 2,並且在正確使用打印機時仍遇到一些麻煩。我做了以下的自定義管道,可以幫助我遍歷對象:Typescript TS7017:隱式地用於Angular 2中的任何管道

import { Pipe, PipeTransform } from '@angular/core'; 

@Pipe({ name: 'mapToIterable' }) 
export class MapToIterablePipe implements PipeTransform { 
    transform(dict: Object): any { 
     var a:any = []; 
     for (var key in dict) { 
      if (dict.hasOwnProperty(key)) { 
     --->  a.push({ key: key, value: dict[key] }); 
      } 
     } 
     return a; 
    } 
} 

當我編譯這個,但是我得到一個「錯誤TS7017:對象類型的指數簽名隱含有一個‘任意’類型的,我真的不明白我怎麼能寫我的功能,以這樣的方式,這種錯誤消失

回答

0

問題在於這一行:

var a:any = []; 

您正在定義aany類型,但使用它,如果它被定義爲數組a.push(...)。您可以使用以下任一選項修復它:

// explicitly define a as an array of any's 
var a: any[] = []; 
// allow TypeScript to infer the type as an array 
var a = []; 
+0

嗯,這並沒有伎倆。錯誤信息也指第9行,即我放置箭頭的那一行。您的解決方案不斷給我:錯誤TS7017:對象類型的索引簽名隱式地具有'任何'類型。 – hY8vVpf3tyR57Xib

+0

似乎在操場上工作正常,也許你有本地的另一個問題(也許你的tsconfig.json)? – Brocco