Reputation: 1069
I have an object that I am trying to cast from my webapi call.
So lets say this is the data from the webapi
let userTestStatus: { id: number, name: string }[] = [
{ "id": 0, "name": "Available", "date" : "01/01/2001" },
{ "id": 1, "name": "Ready", "date" : "01/01/2001" },
{ "id": 2, "name": "Started", "date" : "01/01/2001" }
];
This is my typescript Class. As you can see there is no date property
export myClass{
id: number;
name: string;
}
When the object is converted the date property is there even though it doesn't exist in my class. How can I get typescript to ignore properties that do not exist in the class?
Upvotes: 0
Views: 2235
Reputation: 1511
You can map all of your objects just to such model which you would like to have
const myModelObejctsArray: MyModel[] = userTestStatus.map(user => {
return {
id: user.id,
name: user.name
}
})
Upvotes: 2
Reputation: 18
Your data fulfills the given type interface. However, your data has even more properties than you declared as a type.
Your Typescript IDE should display you some message that the objects in your array do not specifically have a date property. But you can still run it.
If you do not want the date property in your array elements, you could delete it on your own:
userTestStatus.forEach(e => delete e.date);
Upvotes: 0