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

339
Views
Typescript: function parameter have type 'never' when inferred from control flow

Typescript is not resolving the correct parameter type:

functionBuilder takes a parameter arg and depending on that parameter it returns an object which contains a function property. That function takes a string parameter if arg === 'a' else it takes a number parameter.

const functionBuilder = (arg: string) => {
  if(arg === 'a'){
    return {
      f: (val: string) => {}
    }
  }
  return {
    f: (val: number) => {}
  }
}

const f1 = functionBuilder('a');

The problem is that calling that function f on f1 expects an argument of type never instead of string

problem

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I found that the type of functionBuilder was detected as

const functionBuilder: (arg: string) => {
    f: (val: string) => void;
} | {
    f: (val: number) => void;
}

which is the expected type. Is it not type that you want? Like, do you want special treatment for "a"?

Anyway, consider the following (the type of f1.f is the same as Type3):

type Type1 = ( (val:string) => void ) ;
type Type2 = ( (val:number) => void ) ;
type Type3 = Type1 | Type2 ;
let TestVar: Type3;
TestVar(3); //Error: Argument of type 'number' is not assignable to parameter of type 'never'.

It may seem odd at first, but isn't it exactly what "|" is supposed to do? "|" means that the TestVar can be either Type1 or Type2. The only operations that are safe are operations that satisfy BOTH Type1 and Type2 requirements. Number 3 does not fulfil Type1 requirement, whereas string '3' does not fulfil Type2 requirement. Indeed, no argument can fulfil both Type1 and Type2. So it's really "never".

about 4 years ago · Juan Pablo Isaza Report

0

@qrsngky is spot on: there is no value that you can pass safely into ((val: string) => void) | ((val: number) => void) without knowing which of the two functions it is. To solve this, you can cast the result of the functionBuilder call to the one you expect:

const f1 = functionBuilder('a') as { f(val: string): void };
f1.f('…');

(it might help to declare a generic helper type for this, like here)

about 4 years ago · Juan Pablo Isaza Report

0

I tested it. It seems to compile once you add the return type as any.

 const functionBuilder = (arg: string) : any => {
  if(arg === 'a'){
    return {
      f: (val: string) => { console.log(val)}
    }
  }
  return {
    f: (val: number) => {}
  }
} 

The code to executed:

const f1 = functionBuilder('a');
 
f1.f("a");

The console will emit the value 'a'.

about 4 years ago · Juan Pablo Isaza 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!