Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

350
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda