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

169
Visualizações
Using an enum and a payload map to get an implicit second argument of undefined

I'm currently writing a function that takes two arguments. The first being the key of an enum, and the second depends on the first argument.

Some of the keys don't require a second argument, so when I assign undefined in the payload map, the function explicity requires to pass in undefined.

Is there a way I can pass in the first key, and not pass in a second argument, while fulfilling the type requirements?

Code:

export enum MyKey {
  BleepBloop = 'BleepBloop',
  BeepBoop = 'BeepBoop',
  BingBong = 'BingBong',
}

export type SubstitutionsMap = {
  [MyKey.BleepBloop]: [string, string];
  [MyKey.BeepBoop]: [string, string];
  [MyKey.BingBong]: undefined;
};

export const getFoo = <K extends MyKey>(key: K, substitutions: SubstitutionsMap[K]): string => {
    // Handle private logic
    return ''
};

getFoo(MyKey.BingBong) // <--- Error is here

Error:

(enum member) MyKey.BingBong = "BingBong"
Expected 2 arguments, but got 1.ts(2554)
index.ts(13, 49): An argument for 'substitutions' was not provided.

This error is gone when I explicitly pass in undefined, but I would like to not have to pass in a second argument if it's just undefined.

This fixes my error:

getFoo(MyKey.BingBong, undefined)
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You could create function overloads for getFoo that matches your required types.

Something like below:

function getFoo(key: MyKey.BingBong): string;
function getFoo<K extends MyKey.BeepBoop | MyKey.BleepBloop>(
  key: K,
  substitutions: SubstitutionsMap[K]
): string;
function getFoo(key: MyKey, substitutions?: SubstitutionsMap): string {
  if (key === MyKey.BingBong) {
    // code in which I don't need substitutions
  }

  if (substitutions) {
    // code in which I do need to access substitutions
  }

  return '';
}

getFoo(MyKey.BleepBloop);                 //error!
getFoo(MyKey.BleepBloop, ['asd', 'qwe']); //fine
getFoo(MyKey.BeepBoop);                   //error!
getFoo(MyKey.BeepBoop, ['asd', 'qwe']);   //fine
getFoo(MyKey.BingBong);                   //fine
getFoo(MyKey.BingBong, ['asd', 'qwe']);   //error!

This will make it so that when the first argument is MyKey.BingBong, it will not let you pass a second argument, but if it is another member of MyKey, it will require you to pass a second argument.

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