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

297
Visualizações
Why is this expression not callable, if it is only initialized in a Promise?
let callback1: null | (() => void) = () => {};
let callback2: null | (() => void) = null;
let callback3: null | (() => void) = null;

if (callback1 != null)
    callback1(); // callable

callback2 = () => {}
if (callback2 != null)
    callback2(); // callable

new Promise<void>(() => callback3 = () => {});

if (callback3 != null)
    callback3(); // not callable
    // ^^^
    // This expression is not callable.
    //     Type 'never' has no call signatures.

Playground

It seems like TypeScript is doing some static analysis, but wrongly, as the code works fine in JS:

let callback3 = null;
new Promise(() => callback3 = () => { console.log("It Works"); });
if (callback3 != null)
    callback3();

// output: It Works
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

Since you init callback3 with a literal null and you don't modify it, TS infers its type as null.

with if (callback3 != null) you're narrowing it down to never hence the error.

This doesn't happen in this kind of case :

declare const maybeCallback: null | (() => void);

callback3 = maybeCallback;

if(callback3 !== null) {
    // () => void not never
}

Playground

about 4 years ago · Santiago Gelvez 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