Skip to content
Home » Blog » Narrowing Union Types With Typescript Predicates and Assertions

Narrowing Union Types With Typescript Predicates and Assertions

  • by

Imagine making an API request to an endpoint that gives information organized into folders. Requesting the info from a selected folder would return objects that the folder comprises in addition to further folders nested straight underneath the folder in query.

A folder navigation UI would wish to separate aside the Folders from the Items in an effort to render correctly. This may simply be solved in a element by utilizing filter to do away with the undesired objects on the level of use however at the price of iterating the set for every use.

Also, what if I wanted to separate other forms of arrays? For instance, what if I had a listing of customers with an isActive property, and I needed to separate these into [activeUsers, inactiveUsers]? The underlying downside is fairly basic so a small utility operate would assist right here.

Problem: Given an array of two sorts of issues (T and U), let’s cut up them aside and return every subsection of the array in a tuple ([T[], U[]]).

The features works, however the compiler can’t really inform if the splitFn is narrowing the categories appropriately so it complains. There are two major methods to offer it further context.

Solution 1: Type Assertions

The most blatant resolution is simply to inform the compiler what’s taking place through a sort assertion. These assertions are accomplished utilizing the as key phrase.

Generally, type assertions are discouraged as they’ll simply cut back the effectiveness of the compiler by overriding it erroneously. That’s true right here as effectively: offering a splitFn that doesn’t precisely or exhaustively cut up the categories will trigger issues downstream.

Solution 2: Type Predicates

In programming, a “predicate” is a operate with a single parameter that returns both true or false. Typescript uses predicates to narrow types. It’s annotated with a particular return description utilizing the is key phrase.

The splitFn above is already functioning as predicate, so updating the sort definition will clear the error and slender the sort for the compiler.

This clears the error as effectively, however there are a few caveats:

Caveat 1: This method makes extra sense when splitting aside an array into two strongly differentiated subtypes represented within the sort system. In the second instance (the energetic customers) the next works, however the predicate operate has a tough time describing the intent when narrowing inside a sort:

Here it will most likely be higher to create ActiveUser and InactiveUser sorts. However, sure subgroupings resist sort descriptions. There’s no straightforward method that I do know of to interrupt aside a listing of customers into newUsers and oldUsers the place the differentiator is a check for if an accountCreated timestamp occurs earlier than or after a selected time.

Caveat 2: While the sort security right here is stronger than in resolution 1, there’s nonetheless no assure that the predicate is error-free in its differentiation logic. A poorly constructed predicate operate may nonetheless end in compiler errors downstream if it fails to precisely differentiate between the categories.


As neither of those two approaches ensures sort security, I haven’t arrived at a robust conclusion for recommending use. I’ve opted for utilizing sort assertions on condition that it feels extra like idiomatic Javascript to me and there’s much less strain to create new sorts simply to fulfill the compiler when working on single-type arrays. In much less generalized utilities, I’d most likely go for predicates. Which fashion do you utilize?

Leave a Reply

Your email address will not be published. Required fields are marked *