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

300
Visualizações
Can I pick type of return data by argument in TypeScript?

can we predict types by function argument passed to it ?

Let's imagine we have fn that will accept one argument (number) and depends on that number I will return collection or single entity from database. Result will differ in model slightly and I would tell to TypeScript that if such number passed to fn argument exists, return me model A otherwise return me model B.

Fn example:

const fn = (id?: number) => {
  // body of fn
}

const myCollectionData = fn(); // interface A
const mySingleRecordData = fn(1); // interface B

Cheers!

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

0

Sure, you can declare overloads for that function:

const fn: {
  (): interfaceA;
  (id: number): interfaceB;
} = (id?: number) => {
  // body of fn
};

(see Typescript overload arrow functions for this particular 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