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

250
Visualizações
Is there a way in typescript to pass an optional parameter to a constructor only if it exist?

I have an object that has a constructor with an optional parameter like this:

class example{

   constructor(a: string,b: string,c: string,d?:string){
     //...
   }

}

This class will be instatiated inside another method. Like this:

function createExample(a: string,b: string,c: string,d?:string){
  return new example(a,b,c,d)
}

But i only want to pass the "d" parameter if the function createExample() received. Is there a way in javascript/typescript to do this. Something like:

I want to pass the parameter d only if it exists, and I hope I don't have to do an if that checks this, because if I have more optional parameters it can get complicated. Is there a way in javascript/typescript to do this. Something like:

function createExample(a: string,b: string,c: string,d?:string){
  return new example(a,b,c,d?)
}

Or should I just leave the instatiaton with the normal "d" parameter, and in case it is not defined, it should be passed as undefined?

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

0

If you define the type for D as potentially undefined, you can just pass it in

class Example {
   constructor(a: string, b: string, c: string, d?: string) {
     // ...  
   }
}

const A = "";
const B = "";
const C = "";
let D: string | undefined;

if (someCondition) {
  D = "d";
}

const ex = new Example(A, B, C, D);
about 4 years ago · Juan Pablo Isaza Relatório

0

Or should I just leave the instatiaton with the normal "d" parameter, and in case it is not defined, it should be passed as undefined?

Yes, exactly this. JavaScript treats missing arguments and undefined being passed as an argument the same1 - both end up with an undefined or defaulted parameter. So it's as simple as

function createExample(a: string, b: string, c: string, d?: string) {
  return new Example(a, b, c, d);
}

If you really care, you can use spread syntax with an array:

function createExample(...args: [a: string, b: string, c: string, d?: string]) {
  return new Example(...args);
}

1: except for arguments.length and rest parameter syntax

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