A little old but I felt I could add some clarity to this.
Exact Answer
interface MyObject {
id: number;
name: string;
}
interface MyExactData {
[key: string]: MyObject;
}
let userTestStatus: MyExactData = {
"0": { "id": 0, "name": "Available" },
"1": { "id": 1, "name": "Ready" },
"2": { "id": 2, "name": "Started" }
};
But the above is not how we usually do an array of objects, you would use the simple native array in javaScript.
interface MyObject { // define the object (singular)
id: number;
name: string;
}
let userTestStatus_better: MyObject[] = [
{ "id": 0, "name": "Available" },
{ "id": 1, "name": "Ready" },
{ "id": 2, "name": "Started" }
];
Simply adding []
to our interface provides the typing for an array of said objects.
And to do that inline
let userTestStatus_inline: {id:number, name:string}[] = [
{ "id": 0, "name": "Available" },
{ "id": 1, "name": "Ready" },
{ "id": 2, "name": "Started" }
];
I would use the interface as you have something that is definable, understandable, and reusable. If you need to make changes, you can make changes to the one interface and typescript will report your interface to code mismatches.