2

I have a User class with a nested Address class. In the component I would like to initialize an object of type User with something like let user = new User() or let user:User.

But errors are thrown, because Angular/Typescript wants me to assign values. Is there an efficient way to do this, without assigning all the empty "" values to the object.

Below the classes:

class Address {
    constructor(
        public street: string,
        public number: number,
        public suffix: string,
        public houseLetter: string,
        public city: string,
        public zipcode: string,
    ) { }
}

export class User {
    constructor(
        public id: number,
        public firstName: string,
        public lastName: string,
        public email: string,
        public address: Address,
    ) { }
}

4 Answers 4

2

You can mark these parameters as optional like this:

export class User {
    constructor(
        public id?: number,
        public firstName?: string,
        public lastName?: string,
        public email?: string,
        public address?: Address,
    ) { }
}

now you can easily instantiate user object with let user = new User()

Working demo link

Sign up to request clarification or add additional context in comments.

Comments

0

you can try like this

Model

export class AddressModel {
    constructor(data?: any) {
    if (data != null) {
        this.street = data.street;
        this.number = data.number;
        this.suffix = data.suffix;
        this.houseLetter = data.houseLetter;
        this.city = data.city;
        this.zipcode = data.zipcode;
    }
 }
        public street: string = null;
        public number: number = null;
        public suffix: string = null;
        public houseLetter: string = null;
        public city: string = null;
        public zipcode: string = null;
}

TS

someFunction() {
let data = new AddressModel({data to pass}); // here we are passing the data
}

Comments

0

I use this pattern:

export class Address {
    street: string;
    number: number;
    suffix: string;
    houseLetter: string;
    city: string;
    zipcode: string;

    constructor(values? : Partial<Address>)
    { 
        Object.assign(this, values);
    }
}

export class User {
    id: number;
    firstName: string;
    lastName: string;
    email: string;
    address: Address;
    constructor(values? : Partial<User>)
    { 
        Object.assign(this, values);
    }
}

Usage:

const p1 = new User();

const p2 = new User({
  firstName: 'paul',
  address: new Address({
    street: '1 st james street'
  })
});

Comments

0

This seems to work fine:

export class User {
  public id: number = 0;
  public firstName: string = "";
  public lastName: string = "";
  public email: string = "";
}

In TS:

user = new User();
 constructor() {
  console.log(this.user); // {"id":0,"firstName":"","lastName":"","email":""}
}

Online_Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.