Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

146
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda