Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to filter an interface using conditional types in TypeScript?

I’m trying to create a type that takes an interface as a generic type parameter and filters out properties whose values are not strings.

This is what I have:

interface Bla {
    key1: string;
    key2: number;
    key3: string;
    key4: boolean;
}

type PickStrings<T> = {
    [K in keyof T]?: T[K] extends string ? T[K] : never;
}

const bla: PickStrings<Bla> = { // should error as key3 is not present 
    key1: 'string',
}

Playground

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The problem with using [K in keyof T]? is it doesn’t error as keys become optional. But, if I use [K in keyof T] it is checking for the existence of all keys in the interface.

How can I fix this?

>Solution :

You can create a type to extract the keys by type and then use it with Pick

type PickKeysByValueType<T, TYPE> = {
    [K in keyof T]: T[K] extends TYPE ? K : never
}[keyof T]

type PickStrings<T> = Pick<T, PickKeysByValueType<T, string>>

Playground

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading