I have this simple example below where I want to convert an array of objects to an array of Animal objects but I get this error Type 'Animal[]' is missing the following properties from type 'Promise<Animal[]>': then, catch, [Symbol.toStringTag] in the Controller.getPersons() function. I am not entirely sure what is causing this error.
class Animal {
name: string;
colour: string;
constructor(name: string, colour: string) {
this.name = name;
this.colour = colour;
}
}
The class where I have this function which promise to return an array of Animal objects getPersons(): Promise<Animal[]>
class Controller {
data: { name: string; colour: string }[];
constructor(data: { name: string; colour: string }[]) {
this.data = data;
}
getPersons(): Promise<Animal[]> {
const animals = this.data.map(a => new Animal(a.name, a.colour));
console.log("animals -----> ", animals);
console.log("type -----> ", typeof animals);
return animals;
}
This is my sample data which I want to convert to an array of Animal objects
const data = [
{ name: "Dog", colour: "Black" },
{ name: "Cat", colour: "White" }
];
const c = new Controller(data);
c.getPersons();
I would appreciate any help. Thank you in advance.
Animal[]as opposed toPromise<Animal[]>. Either alter the return type ofPromise<Animal[]>to beAnimal[]or updatedatato return a promise.