2016-07-05 10 views
2

私はTypescriptとjavascriptを初めて使用しています。 私の質問は: このJSONオブジェクトをTypeScriptオブジェクトに自動的にマップする(角2)

`` `

{ 
    "release": { 
     "ref": {}, 
     "commits": {} 
    }, 
    "debug": { 
     "ref": {}, 
     "commits": {} 
    }, 
    "feature": { 
     "ref": {}, 
     "commits": {} 
    }, 
    "hotfix": { 
     "ref": {}, 
     "commits": {} 
    } 
} 

` ``

マップからそれ活字体使用してインタフェースを持つと

export interface IRepo { branches: IBranch; // how to make branches a of type string: IBranch dictionary? }

未知の方法があるでしょうデバッグ、リリースなどのプロパティの数 私はそれらを保ちたいIRepoインスタンス

で辞書として私は、データをフェッチするためにこれを使用しています:

`` `

getRepo() : Observable<IRepo> { 
    if (!this.repo) { 
     return this.http.get(this._baseUrl + 'repo.json') 
      .map((res: Response) => { 
       this.repo = res.json(); 
       return this.repo; 
       }) 
       .catch(this.handleError); 
    } else { 
     return this.createObservable(this.repo); 
    } 
} 

` ``

+0

プロパティの種類は何ですか?強く型付けする必要がない場合は、for-inでオブジェクトのプロパティを繰り返し処理できます。コンパイル時に厳密に型指定されたそれぞれを必要とする場合は、それらが持つタイプを指定できますか(共通するものがあれば) –

答えて

3

あなたが知られているいくつかのから行くのマップを持っているrefcommitsを有する)と比較して、文字列インデクサーを使用してください:

interface BranchByName { 
    [branchName: string]: { 
    ref: any; 
    commits: any; 
    } 
} 
関連する問題