4

i'm new to Typescript and javascript. My question is: how from this

```

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

```

map it with with Typescript using interfaces

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

there will be unknown number of properties like debug, release, etc, i want to keep them as dictionary in IRepo instance

i'm using this to fetch data:

```

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);
    }
}

```

1
  • What are the types of the properties? If you don't need them strongly typed, you can just iterate over the properties of the object with for-in. If you need each of them strongly typed at compile time, can you specify what type(s) they'll have (if anything in common) Commented Jul 6, 2016 at 0:41

1 Answer 1

6

You have a map that goes from some known string to an object of known structure (has ref commits). Just use a string indexer:

interface BranchByName {
  [branchName: string]: {
    ref: any;
    commits: any;
  } 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.