Samantha J T Star
Samantha J T Star

Reputation: 32838

What is the typescript definition like for a an array of object?

I have this javascript object:

subjects = {
    "1": { "id": 1, "name": "City" },
    "100": { "id": 100, "name": "Test" }
};

Can someone help me by telling me how I could define this in typescript. I know I can use "any" but I would like to do this properly

Upvotes: 1

Views: 61

Answers (2)

David Driscoll
David Driscoll

Reputation: 1419

If you're looking to be able to iterate over the array using a for loop, you'd need to define length as well.

Then in theory you could define it as subjects : { id: number; name: string; }[] = <...>

If you don't want to iterate over it, then you could just define it as subjects : { [key: string] : { id: number; name: string; } } = <...>

Upvotes: 1

Altay Mazlum
Altay Mazlum

Reputation: 442

subjects list:number[] = [1, 2, 3];

or

subjects list:Array<number> = [1, 2, 3];

Upvotes: 1

Related Questions