Home > front end >  Type guard function for the `[key, value]` entry
Type guard function for the `[key, value]` entry

Time:12-28

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?

CodePudding user response:

does this work for you?

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