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

228
Visualizações
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 à 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