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

263
Views
Typescript error when multiple optional types are used

This an example of the error:

interface Block {
  id: string;
}

interface TitleBlock extends Block  {
  data: {
    text: "hi",
    icon: "hi-icon"
  }
}

interface SubtitleBlock extends Block  {
  data: {
    text: "goodbye",
  }
}

interface CommentBlock extends Block {
  author: string; 
}

type BlockTypes = TitleBlock | SubtitleBlock | CommentBlock

function mapper(block : BlockTypes) {
    if(block?.data?.text){  // TS Error here!!! 
      /** do something */
    }
}

Live example

I want to do something in the function when the block has a specific attribute but TS does not allow me to do so even when there are type in BlockTypes that have that property.

What is the best way to manage this types of functionalities?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The optional chaining operator (?.) only guards against null and undefined; it does not filter a union-typed expression to those members known to contain that key. See this comment in Microsoft/TypeScript#33736 for a canonical answer.


The issue is that object types in TypeScript are open and are allowed to contain more properties than the compiler knows about. So if block.data is truthy, it unfortunately does not imply that block.data.text exists:

const oof = {
  id: "x",
  author: "weirdo",
  data: 123
}
mapper(oof); // accepted

Here oof is a valid CommentBlock, and so mapper(oof) is allowed, and oof.data is truthy, but oof.data.text is undefined, and you will have a problem if you try to treat it like a string.

So it would be unsound (that is, not type safe) to allow optional chaining to filter unions the way you want.


The workaround here, if you don't think that oof-like cases are likely, is to use the in operator as a type guard:

function mapper(block: BlockTypes) {
  if ("data" in block) {
    block.data.text.toUpperCase()
  }
}

This works fine with no compiler error. Of course, it's still unsound in exactly the same way as narrowing with ?. would be... if you call mapper(oof) there are no compiler errors but you will get a runtime error. So you should be careful.

It's a little weird that TypeScript allows in to behave this way but does not allow other similar constructs. You can read the discussion in microsoft/TypeScript#10485 to see why that happened. Mostly it seems to be that when people write "foo" in bar they rarely do the wrong thing with it, but that other constructs like bar.foo && bar.foo.baz are misused more often. But that's according to the TS team, not me. 🤷‍♂️

Playground link to code

about 4 years ago · Juan Pablo Isaza Report
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!