Skip to content

HasNonNullish? #1311

@stevenvachon

Description

@stevenvachon

Type description + examples

Determine if a possibly nullish object contains a property.

It could be paired with something like this:

const hasOwnNonNullish = <T extends object, K extends keyof T>(
  object: T | null | undefined | void,
  property: K
): object is NonNullishProp<T, K> =>
  object != null &&
  Object.prototype.hasOwnProperty.call(object, property) &&
  (object as any)[property] != null;

Type source

export type NonNullishProp<T, K extends keyof T> = T & {
  [P in K]-?: Exclude<T[P], null | undefined | void>;
};

Search existing types and issues first

  • I tried my best to look for it

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions