Here is the code I'm working with:
export class People {
name: string;
age: number;
constructor(data: any) {
this.name = data.name;
this.age = data.age;
}
}
I keep encountering an error when using 'any'. What should be the appropriate type for data
to avoid any errors?
I am aware that I can also write it as:
export class People {
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
}
However, I want this model to be able to handle any JSON object, like one retrieved from a server, so that I can do:
const data = await fetchData();
const person = new Person(data)
What modifications should I make in order to continue using the model for any object and eliminate all TypeScript errors?
I attempted changing the type to: object
, but then encountered errors on data.name
and data.age