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

348
Vistas
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 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