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

Type guard function for the `[key, value]` entry

const isNonNullable = <V>(value: V): value is NonNullable<V> =>
  value !== null && value !== undefined

How to make a generic type guard function like this one, but for the ([key, value]) entry?

I want to use it like this: Object.entries(foobar).filter(isNonNullableEntry).

const isNonNullableEntry = <T extends [K, V]>(entry: T): entry is [K, NonNullable<V>] =>
  entry[1] !== null && entry[1] !== undefined

The above is my incorrect attempt. How to make it right?

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

>Solution :

does this work for you?

const isNonNullableEntry = <K,V>(entry: [K, V]): entry is [K, NonNullable<V>] =>
  entry[1] !== null && entry[1] !== undefined
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