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

346
Views
How to infer function overloading TS Types, when args are functions with optional args

I want to implement a pattern similar to React.useState(). Basically, the function would accept one of 2 things:

  • A {string} value to update the state to.
  • A {function} that accepts the previousState and produces a new one.

Here is an example not that complex. The interface din:

interface OverloadedFn<TArgs>  {
    (args: TArgs): void;
    (setter: (args: TArgs) => TArgs): void;
}

const overloadedFn: OverloadedFn<{ arg1: number }> = (arg) => {
    if (typeof arg === 'function') {
      ...
    } else {
      ...
    }
}

I can write the overload, with multiple function, with type or with interface, but the args, will never get typed. I even removed the Generic. Any ideas?

TS7006: Parameter 'arg' implicitly has an 'any' type.

Here is a minimal reproducible example, in TS Playgound: https://www.typescriptlang.org/play?ts=4.1.5#code/JYOwLgpgTgZghgYwgAgPIDdoBsD2cAmE+AYiADwAqAglAOYDOAfMgN4CwAUMsgBRx30AXMmoCAlMPpgooWgG5O3HvQhhIUYXwHDRDMcgC8zXfQnIpMkPM4BfTpwQ4QU5DkxRcBIqWEZseQhJyFmR+WgBGYRAAVwBbACNoZBtmA14w00Nmdi5kYBheMABPAAcIHAKMwwM0gHIYaJAEMGAnWv0c7i7kKFVoqBBkWvDaxWTkCCwVVjHu3rB+wdqAJlHcuw4bOSA

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!