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

194
Views
Is it possible to create a type-safe function that receives a callback and the callback's params with TypeScript?

I am trying to create a function that receives two arguments: a callback function, and the parameters (props) for that function. For example:

const callbackFunction = (a: number) => a + 2;
const callbackFunctionProps = 5;
callbackWrapper(callbackFunction, callbackFunctionProps);

I want TypeScript to check that the props given as 'callbackFunctionProps' are valid props for 'callbackFunction'. Is there a way to do this without using generics? it does not matter what are the types of the function and props as long as they fit each other.

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Generics are exactly the remedy for "it does not matter what are the types" cases. Here's one possible way to do that with generics:

const callbackWrapper = <I extends Array<unknown>, O>(fn: (...fnArgs: I) => O, ...args: I): O => 
{
    const result = fn(...args);
    console.log(`CALLED WITH ${args.join(', ')}, RESULT IS ${result}`);
    return result;
}

const callbackFunctionA = (a: number) => a + 2; 
const callbackFunctionPropsA = 5;

const callbackFunctionB = (a: number, b: number) => a + b + 2;
const callbackFunctionPropsB1 = 10;
const callbackFunctionPropsB2 = 30;

callbackWrapper(callbackFunctionA, callbackFunctionPropsA); 
// "CALLED WITH 5, RESULT IS 7" 
callbackWrapper(callbackFunctionB, callbackFunctionPropsB1, callbackFunctionPropsB2);
// "CALLED WITH 10, 30, RESULT IS 42"

The trick is using I (type-param of created generic) in two places: as a type of passed function's param (...fnArgs: I part) and as a type of params passed into your callback (...args: I part). If they do not match, the compiler will yell at you:

callbackWrapper(callbackFunctionA, '2'); // Argument of type 'string' is not assignable to parameter of type 'number'
callbackWrapper(callbackFunctionA, 2, 3); // Expected 2 arguments, but got 3.
callbackWrapper(callbackFunctionB, 2); // Expected 3 arguments, but got 2.
about 4 years ago · Santiago Trujillo Report

0

function callbackWrapper<T extends any[]>(
  callbackFunction: (...arg: T) => any,
  ...callbackFunctionProps: T
) {
  console.log(callbackFunction(...callbackFunctionProps));
}

function callbackFunction1(a: number) {
  return a + 2;
}
const callbackFunctionProps1 = 5;
callbackWrapper(callbackFunction1, callbackFunctionProps1);

function callbackFunction2(a: string) {
  return a + ' world';
}
const callbackFunctionProps2 = 'hello';
callbackWrapper(callbackFunction2, callbackFunctionProps2);
about 4 years ago · Santiago Trujillo Report
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!