Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!