Implement a NonNullish<T> Conditional Type in TypeScript

Share this video with your friends

Send Tweet

In this lesson, we're going to learn about conditional types, a powerful feature of TypeScript's type system. Conditional types let us express non-uniform type mappings, that is, type transformations that differ depending on a condition.

A conditional type describes a type relationship test and selects one of two possible types, depending on the outcome of that test. It always has the form T extends U ? X : Y, which makes conditional types look similar to conditional expressions in JavaScript.

We're going to use a conditional type to implement a NonNullish<T> helper type. This type is identical to the NonNullable<T> helper type which is defined in the core type declaration files that ship with the TypeScript compiler. Step by step, we're going to resolve an application of the NonNullish<T> type to understand how conditional types are being evaluated.

Additional Reading