2016-07-05 2 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

당신은 어떤 알려진에서가는지도를 string은 알려진 구조의 대상 (refcommits)과 일치합니다. 문자열 인덱서 만 사용하십시오.

interface BranchByName { 
    [branchName: string]: { 
    ref: any; 
    commits: any; 
    } 
} 
관련 문제