1
/**
2
 * Returns true if `val` is not `null` or `undefined`
3
 */
4 1
export const isDefined = <T>(val: T): val is NonNullable<T> => val !== undefined && val !== null
5

6
/**
7
 * Returns a function that returns `true` if the given `key` of the object is not `null` or `undefined`.
8
 */
9 1
export const propertyIsDefined = <T extends object, K extends keyof T>(key: K) => (
10
    val: T
11 1
): val is K extends any ? T & { [k in K]: NonNullable<T[k]> } : never => isDefined(val[key])

Read our documentation on viewing source code .

Loading