In the below syntax,
interface IPerson{
firstName: string;
lastName?: string;
}
const personList = [
"p1": {firstName: "F1", lastName: "L1"},
"p2": {firstName: "F2"},
"p3": {firstName: "F3"}
];
// or
const personList = [
{"p1": {firstName: "F1", lastName: "L1"}},
{"p2": {firstName: "F2"}},
{"p3": {firstName: "F3"}}
];
personList is an array of key:value pairs, where key is of type string and value is of type IPerson
Edit:
Below syntax allows more than one key:value pair at an index of an array
const personList:{[key:string]:IPerson}[] = [
{
"p1": {firstName: "F1", lastName: "L1"},
"p2": {firstName: "F1", lastName: "L1"}
},
{"p2": {firstName: "F2"}},
{"p3": {firstName: "F3"}}
];
How to explicitly type personList?
const personListis not a valid JS array.