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

239
Visualizações
How to specify single optional parameter value when calling a function

I have the following function:

function myFunc(
  id: string,
  optionalParamOne?: number,
  optionalParamTwo?: string
) {
  console.log(optionalParamTwo);
}

I would like to call this function by specifying the id as it is required and optionalParamTwo. I don't care about optionalParamOne. I cannot change this function.

How can I call this function with the parameters id and optionalParamTwo without having to specify optionalParamOne?

I could do this:

myFunc('abc', null, 'dog')

But I have many optional parameters in my function signature and I'd rather not specify a value for each.

Demo

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

0

You must provide some value (even if its undefined) for every ordered parameter before you can provide a value for a higher-indexed parameter.

The common pattern for reducing boilerplate when invoking functions with uninteresting parameters is to curry:

TS Playground

function myFunc (
  id: string,
  optionalParamOne?: number,
  optionalParamTwo?: string
) {
  console.log(optionalParamTwo);
}

// Create a parameter-expanding function:
const myParams = (id: string, optionalParamTwo?: string) => [id, undefined, optionalParamTwo] as const;
//                                                               ^^^^^^^^^
// Include undefined for the params you don't want to think about when calling the function

myFunc(...myParams('abc', 'dog')); // logs "dog"

// Or just curry it:
const myFuncWithFewerParams = (
  id: string,
  optionalParamTwo?: string,
) => myFunc(id, undefined, optionalParamTwo);

myFuncWithFewerParams('abc', 'cat'); // logs "cat"

For sliced tuples (addressing partial parameters) in the type system, see this answer.

about 4 years ago · Juan Pablo Isaza Relatório

0

It's not possible to do so without changing the function. You can write a wrapper around this function with destructuring assignment like so to achieve the desired result:

function myFunc(
  id: string,
  optionalParamOne?: number,
  optionalParamTwo?: string
) {
  console.log(optionalParamTwo);
}

interface MyFuncParams {
  optionalParamOne?: number,
  optionalParamTwo?: string
}

function myFuncWrapper(id: string, { optionalParamOne, optionalParamTwo }: MyFuncParams = {}) {
  myFunc(id, optionalParamOne, optionalParamTwo);
}

myFuncWrapper('abc', { optionalParamTwo: 'dog' });
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