TypeScript: Create a condition-based subset types

Deep dive into typing system to solve THE ultimate riddle

Photo by Alvaro Reyes on Unsplash

TL;DR; Source code of experiment. Solution.

In this article, we’re going to experiment with TypeScript 2.8 conditional and mapping types. The goal is to create a type that would filter out all keys from your interface, that aren’t matching condition.

You don’t have to know details of what mapping types are. It’s enough to know that TypeScript allows you to take an existing type and slightly modify it to make a new type. This is part of its Turing Completeness.

You can think of type as function — it takes another type as input, makes some calculations and produces new type as output. If you heard of Partial<Type> or Pick<Type, Keys> , this is exactly how they work.

📐Let’s define the problem

Say you have a configuration object. It contains different groups of keys like IDs, Dates and functions. It may come from an API or be maintained by different people for years until it grows huge. (I know, I know, that never happens)

We want to extract only keys of a given type, such as only functions that returns Promise or something more simple like key of type number .

We need a name and definition. Let’s say: SubType<Base, Condition>

We have defined two generics by which will configure SubType :

Base — the interface that we’re going to modify.

— the interface that we’re going to modify. Condition — another type, this one telling us which properties we would like to keep in the new object.

Input

For testing purposes, we have Person , which is made of different types: string , number , Function . This is our “huge object” that we want to filter out.

interface Person {

id: number;

name: string;

lastName: string;

load: () => Promise<Person>;

}

Expected outcome

For example SubType of Person based on string type would return only keys of type string:

// SubType<Person, string>



type SubType = {

name: string;

lastName: string;

}

📈Step by step to a solution

Step 1 — Baseline

The biggest problem is to find and remove keys that doesn’t match our condition. Fortunately, TypeScript 2.8 comes with conditional types! As a little trick, we’re going to create support type for a future calculation.

type FilterFlags<Base, Condition> = {

[Key in keyof Base]:

Base[Key] extends Condition ? Key : never

};

For each key, we apply a condition. Depending on the result, we set the name as the type or we put never , which is our flag for keys that we don’t want to see in the new type. It’s a special type, the opposite of any. Nothing can be assigned to it!

Look how this code is evaluated:

FilterFlags<Person, string>; // Step 1 FilterFlags<Person, string> = { // Step 2

id: number extends string ? 'id' : never;

name: string extends string ? 'name' : never;

lastName: string extends string ? 'lastName' : never;

load: () => Promise<Person> extends string ? 'load' : never;

} FilterFlags<Person, string> = { // Step 3

id: never;

name: 'name';

lastName: 'lastName';

load: never;

}

Note: 'id' is not a value, but a more precise version of the string type. We’re going to use it later on. Difference between string and 'id' type: