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

230
Views
TypeScript how to type hint a function

I would like to type hint my function in TypeScript as below:

// would like to remove this type
type FN<TArgs, TReturn> = (args: TArgs) => TReturn;

interface Args {
  itemWeight: number;
  quantity: number;
}

const calcTotalWeight: FN<Args, number> = ({ itemWeight, quantity }) => itemWeight * quantity;

Is there any built-in generic that provides this utility? Currently, I have created one as above, but I feel as if this could be a very common use case. For example, react has FC.

I do note that this type requires your function to be unary, but that's the whole point here. It's common to have a unary function, with the single argument an object so that we can have "named" parameters.

Does anyone know if this exists, or thoughts on whether this is a good idea?

Extension:

Does anyone know how to make the second argument of the generic optional so that it is inferred if not provided? For example:

const calcTotalWeight: FN<Args> = ({ itemWeight, quantity }) => itemWeight * quantity;

// typeof calcTotalWeight === FN<Args, number> , or
// typeof calcTotalWeight === (Args) => number
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!