Skip to content
tezvyn:
Menu

Implement the built-in NonNullable<T> utility type from scratch

Source: typescriptlang.orgMediumHow cards are made

Tests conditional type distribution and union filtering. A strong answer uses distributive conditional types to map null and undefined to never while preserving other union members.

What's really being asked

This tests your grasp of TypeScript's type-level programming, specifically distributive conditional types and how they interact with union types. The interviewer wants to see if you know that conditional types distribute over naked type parameters, which is the standard mechanism for filtering unions. It also checks whether you distinguish between compile-time type transformations and runtime value operations.

The full answer

First, write the one-line implementation: type MyNonNullable<T> = T extends null | undefined ? never : T. Second, explain that because T is a naked type parameter in the extends clause, the conditional type distributes over each member of a union. Third, note that null and undefined each extend null | undefined, so they resolve to never, while other types resolve to themselves. Fourth, mention that never disappears from unions automatically, leaving only the non-nullish types. Fifth, optionally contrast this with a non-distributive version using square brackets like [T] extends [null | undefined] to show you understand how to disable distribution when needed.

The mistakes people make

A mapped type like type X = { [K in T]: K } is incorrect because mapped types iterate over keys, not union members directly, and do not filter nullability. Using value-level JavaScript checks like typeof or instanceof is a red flag because the question asks for a type utility, not a runtime function. Proposing Object.assign or spread operators shows confusion between values and types. Some candidates write T extends null ? never : T extends undefined ? never : T which works but reveals less understanding of union types in the extends clause. Forgetting that naked type parameters distribute and being unable to explain why the type works is also a weakness.

What usually comes next

The interviewer might ask how to implement Exclude or Extract using the same pattern. They may ask what happens if you wrap T in a tuple like [T] extends [null | undefined], which prevents distribution and makes the utility behave differently on unions. They might ask how this differs from the definite assignment assertion operator or from strictNullChecks. Another follow-up is implementing a deep NonNullable that recursively removes null and undefined from nested properties.

A concrete example

Given type User = { name: string } | null | undefined, applying MyNonNullable<User> produces { name: string }. If you had type Status = "active" | "inactive" | null, the result is "active" | "inactive". The never type absorbs away in the resulting union, which is why you do not need explicit subtraction syntax.

Interview question

Given type A<T> = T extends null | undefined ? never : T and type B<T> = [T] extends [null | undefined] ? never : T, what is the difference when each is applied to string | null?

  • a.Type A yields string | null; type B yields string
  • b.Type A yields never; type B yields string
  • c.Type A yields string | null; type B yields never
  • d.Type A yields string; type B yields string | nullCorrect
Why?

Type A distributes over the union because T is naked, so null maps to never and disappears, leaving string. Type B wraps T in a tuple, which stops distribution and causes the whole union string | null to be tested as one type against null | undefined, so it is preserved.

Just read this? Test yourself on what you have been reading.

Read the original → typescriptlang.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on typescript — each one lists the topics its interview covers.

See open roles