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

147
Visualizações
How to keep the typing on callback function in typescript?

foo function returns an object. typescript is know about the typing that return prop1 and prop2 wrap in promise:

const foo: ({ a, b }: {
    a: any;
    b: any;
}) => Promise<{
    prop1: string;
    prop2: string;
}>

But when I wrap foo function with fn function, then b variable that create by the callback is losing the typing and become const b: (a?: any, b?: any, c?: any) => Promise<any>.

My question is how to fix that so typescript can know about the typing? what I try to get is b variable will be the typeof the function I pass. in this case is foo function. Like that:

const b: Promise<{
    prop1: string;
    prop2: string;
}>

I try to use T: const fn = <T>(cb: T) => { but it throw an error on the callback and not sure what is mean. I don't know if I correct about this.

stackblitz

const foo = async ({ a, b }) => {
  console.log({ a, b });

  return {
    prop1: 'prop1',
    prop2: 'prop2',
  };
};

const fn = (cb) => {
  return async (a = null, b = null, c = null) => {
    const results = cb({ a, b });
    if (results) {
      return c();
    }
  };
};

const bar = () => fn(foo);

const b = bar(); // const b: (a?: any, b?: any, c?: any) => Promise<any>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

if I understand you question correctly you can do something like this

const fn = <T>(cb: ({a, b}: any) => Promise<T>) => {
  return async (a = null, b = null, c:() => T | null = null) => {
    const results =  cb({ a, b });
    if (results) {
      return c();
    }
  };
};
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