Using typescript to type an array of objects:
Here are the interfaces:
export interface IUser {
id: string;
order: number;
}
export interface IUsersLogin {
result: number;
UserId: string;
}
export interface IUsersUsers {
id: number;
order: number;
}
export interface IOriginalData {
Users?: IUser[];
UsersLogins?: IUsersLogin[];
UsersUsers?: IUsersUsers[];
}
Here I create an object using this interfaces:
const originalData: IOriginalData = {
Users: [
{
id: "e4e2bb46-c210-4a47-9e84-f45c789fcec1",
order: 1
},
{
id: "b95274c9-3d26-4ce3-98b2-77dce5bd7aae",
order: 2
}
],
UsersLogins: [
{
result: 1,
UserId: "e4e2bb46-c210-4a47-9e84-f45c789fcec1"
},
{
result: 0,
UserId: "b95274c9-3d26-4ce3-98b2-77dce5bd7aae"
}
],
UsersUsers: [
{
id: 1,
order: 0
},
{
id: 2,
order: 0
}
]
};
And here I manipulate the data of this object to push it into another object:
interface IPushedDataItem {
data: IUser | IUsersLogin | IUsersUsers;
}
type TypePushedData = Array<IPushedDataItem>;
let pushedData: TypePushedData = [];
Object.keys(originalData).forEach(item => {
pushedData.push({
data: originalData[item]
});
});
In this process, I cant type pushedData properly, and it complains with data:originalData[item].
You can find this in the TypeScript playgound:
Any help will be welcome!
pushedDatato contain? The type signature you've given it says it expects objects with adataproperty, but your code doesn't do that...