Reputation: 1049
I am trying to create a model from a call to an api that returns objects in the response.
I have a simple class
but now there is an object in the model and I don't know how to create my class
as the school->name
property in not defined on my page and typescript is complaining.
export class User {
title: string;
first_name: string;
last_name: string;
school : object {
name: string
}
}
What is the correct syntax to add the school
object
Thank you
Upvotes: 0
Views: 70
Reputation: 250942
You were close - you just leave out the word "object". Like this:
export class User {
title: string;
first_name: string;
last_name: string;
school: {
name: string
}
}
// Sample usage
const user = new User();
user.school = { name: 'Beacon Hills High School' }
You can also create a User with an object literal:
const user: User = {
title: 'Mr',
first_name: 'Student',
last_name: 'Rik',
school: { name: 'Beacon Hills High School' }
};
Upvotes: 1
Reputation: 38683
try this proper model creation way
export class User {
title: string;
first_name: string;
last_name: string;
school: School;
}
export class School {
name: string;
}
let user = new User();
let school = new School();
school.name = 'Beacon Hills High School';
user.school = school;
Upvotes: 1