Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

221
Visualizações
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).

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

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);
}
about 4 years ago · Juan Pablo Isaza Relatório

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 }
about 4 years ago · Juan Pablo Isaza Relatório

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);
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda