I have an Index Signature with a number as a key and Person object as values. The order of the resulting array is not important.
How is it possible, to create an Array of all Person values from the Index Signature? At the end I am expecting an array containing the two Person objects.
Please see my simple example below and on jsfiddle.
Code:
interface Person {
firstName: string;
lastName: string;
}
let firstUser = {
firstName: "Malcolm",
lastName: "Reynolds"
};
let secondUser = {
firstName: "Tom",
lastName: "Reynolds"
};
let indexSignature: {[key: number]: Person} = {};
indexSignature[158] = firstUser;
indexSignature[2] = secondUser;
// how to get an array of all Person objects in indexSignature?
jsfiddle:
Link
{key: number: Person}is not valid TypeScript syntax. An index signature should look like a computed property like{[key: number]: Person}.indexSignature, just given it a type. SoindexSignature[1] = ...is going to be a runtime error. And indices usually start with0. Are you trying to start with1instead for some reason? If so you might want to mention it so it doesn't look like a typo. Backing up: I think this question could use a good minimal reproducible example where the only issue present is the one you are asking about.