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

227
Vistas
Typescript - ReturnType not working with keyof and function params

Here we can call getValue().b to get computed value:

function foo<
  Computed extends {
    [key: string]: (param1: string, param2: number) => any;
  },
>(options: {
  computed?: Computed;
  callback?: (
    getValue: () => {
      [K in keyof Computed]: ReturnType<Computed[K]>;
    },
  ) => void;
}) {
  return options;
}

foo({
  computed: {
    b: () => 2,
  },
  callback: (getValue) => {
    getValue().b; // work!
  },
});

But it get wrong when i want to define callback parameter type in each computed value:

function foo<
  Computed extends {
    [key: string]: (param1: string, param2: number) => any;
  },
>(options: {
  computed?: Computed;
  callback?: (
    getValue: () => {
      [K in keyof Computed]: ReturnType<Computed[K]>;
    },
  ) => void;
}) {
  return options;
}

foo({
  computed: {
    b: (x, y) => x + y, // add x, y here!
  },
  callback: (getValue) => {
    getValue().b; // not work! the type of b is any!
  },
});

PS: this demo works, but arguments to function foo is not what i want.

function foo<
  Computed extends {
    [key: string]: (param1: string, param2: number) => any;
  },
>(
  computed?: Computed,
  callback?: (
    getValue: () => {
      [K in keyof Computed]: ReturnType<Computed[K]>;
    },
  ) => void,
) {}

foo(
  {
    b: (x, y) => x + y,
  },
  (getValue) => {
    getValue().b; // work!
  },
);

So, my question is, why the last demo worked, but second not?

about 4 years ago · Juan Pablo Isaza
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