Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

350
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!