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

352
Visualizações
Typescript conditional causes error when stored in a variable

Can someone please explain why the first example works in Typescript, but the second example throws an error??? Here is a live example in a Typescript playground.

const {type, payload} = action;

switch(type) {

...

  //*** EXAMPLE 1 ***//

  case "CREATE_VARIABLE":
    if (payload == null || typeof payload === "string" || !("color" in payload)) return state;
    return createVariable(state, payload);

...


  //*** EXAMPLE 2 (Throws error in Typescript) ***//

  case "CREATE_VARIABLE":
    const isNotAVariable = (payload == null || typeof payload === "string" || !("color" in payload));
    if(isNotAVariable) return state;

    return createVariable(state, payload);

...

}

The second example throws the following Typescript error:

Argument of type 'string | Formula | Variable | Constant | undefined' is not assignable to parameter of type 'Variable | undefined'.
  Type 'string' is not assignable to type 'Variable | undefined'
about 4 years ago · Juan Pablo Isaza
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