QuickSnip LogoA light blue logo with a thunderbolt in bracketsQuickSnip

Nullable Keys

Extracts keys from an object type that allow null or undefined values.

  • helper-types
  • typedefinition
  • nullable

Install

npx shadcn@latest add https://quicksnip.dev/r/nullable-keys.json

This snippet is not verified yet. Review it before use, or help verify it.

Code

type NullableKeys<T> = {
    [K in keyof T]: null extends T[K] ? K : undefined extends T[K] ? K : never;
}[keyof T];

Usage

type A = {
    id: string;
    name?: string;
    description: string | null;
};

type Nullable = NullableKeys<A>; // "name" | "description"