Skip to content

Nullish/Nilable type (like Partial, but accepts null) #1285

@bvandercar-vt

Description

@bvandercar-vt

Type description + examples

Partial allows for all properties to be undefined. This type would do that, but also accept null.

export type Nullish<T> = { [K in keyof T]?: T[K] | null }

You may think "why not name it Nullable?" IMO, This would indicate only null, not also undefined. "Nullish" comes from the nullish coalescing operator ??-- "nullish" means "null or undefined"

Other possible names:

  • NullablePartial (but Nullish is shorter and explains it)
  • Nilable ("nil" can also commonly mean "both nul and undefined")

I also think that with this, it would make sense to add:

  • IsNullish (or isNilable, if we go with that naming convention) - like IsOptional or IsNullable, but for both-- Returns a boolean for whether the given type includes null or undefined)
  • NullishKeysOf / NilableKeysOf

Type source

No response

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