• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

155
Views
Default value to properties when value is not assigned

I'm converting form value to object which is passed to API after few logic. But when i convert the properties which doesn't show in the UI on a validation sets its default value to empty even if the data type is number or boolean or date.

//Class:

export class Detail{
id: number;
address: string;
active: boolean;
statusId: number;

 public constructor(init?: Partial<Detail>) {
       
        Object.assign(this, init);
    }
}

//Compoennt:

 let detail = new Detail(this.DetailForm.value);
 console.log(detail)

where in a condition, the active and statusId field is hidden and the user just selects the id and type in address.

//on console log this gives

id:5
address: 'abc xyz'
active: ''
statusId: ''

//Expected output: I expect when the property is not set, it should have its default.

id:5
address: 'abc xyz'
active: false
statusId: 0

//Tried:

  1. Set default value in the property class active: boolean = false

  2. Set default value in the class in constructor.

    init.active = init.active ?? false; init.statusId = init.statusId ?? 0; Object.assign(this, init);

After the Object.assign it turned back to empty

Any ideas to handle such issue for all the data types (like date too).

almost 3 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I would use the nullish operator:

public constructor(init?: Partial<Detail>) {
    init.active = init.active ?? false;
    init.statusId = init.statusId ?? 0;

       
    Object.assign(this, init);
}
almost 3 years ago · Juan Pablo Isaza Report

0

Not sure if I am missing something here but this worked for me.

export class Detail {
  id: number = 5;
  address: string = "abc xyz";
  active: boolean = false;
  statusId: number = 0;

  public constructor(init?: Partial<Detail>) {
    Object.assign(this, init);
  }
}


const test1 = new Detail({ id: 3})
const test2 = new Detail();
const test3 = new Detail({id: 3, statusId: 4, address: "my-address"});
console.log(test1);
console.log(test2);
console.log(test3);

Expected output:

Detail { id: 3, address: 'abc xyz', active: false, statusId: 0 }
Detail { id: 5, address: 'abc xyz', active: false, statusId: 0 }
Detail { id: 3, address: 'my-address', active: false, statusId: 4 }
almost 3 years ago · Juan Pablo Isaza Report

0

Since the values are empty string (not null/undefined), then you can use the OR || operator instead of nullish ??, like the following:

public constructor(init?: Partial<Detail>) {
    init.active = init.active || false;
    init.statusId = init.statusId || 0;
       
    Object.assign(this, init);
}
almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error