Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

346
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda