I have following classes and I would like to initialize citiesRegistered with initial values of city object which contains parks 'Times' and 'Blues' and schools as 'High' and middle but currently it is an empty object
export const initialState: CustomerState = {
customer: new Customer(),
};
export class Customer{
id: number;
age: number;
cars: carClass;
phoneNumbers: string[];
}
export class carClass{
name:string;
citiesRegistered:city[] = []; //This is what makes it empty. How can I put default values of cities in here
}
export class city{
parks: string[] = ['Times','Blues'],
lakes: string[] = [],
schools: string[] = ['High','Middle']
}
I think you may need to change the initialization and default values a bit:
class Car {
name: string = '';
citiesRegistered: City = {
lakes: ['MyLake'],
parks: ['MyPark'],
schools: ['MySchool']
}
}
class Customer {
id: number = 0;
age: number = 0;
car: Car = new Car();
phoneNumbers: string[] = [];
}
const initialState = {
customer: new Customer(),
};
class City {
parks: string[] = ['Times', 'Blues']
lakes: string[] = []
schools: string[] = ['High', 'Middle']
}
console.log(initialState.customer.car.citiesRegistered)
OUTPUT
lakes: ["MyLake"]
parks: ["MyPark"]
schools: ["MySchool"]