Type Alias IsNullable<T>

IsNullable<T>: Extract<T, null | undefined> extends never
    ? false
    : true

Checks if type T is possibly null or undefined.

Type Parameters

  • T

    The type to check if it is nullable.

import { assertType, IsNullable } from "@std/testing/types";

assertType<IsNullable<string | null>>(true);
assertType<IsNullable<string | undefined>>(true);
assertType<IsNullable<null | undefined>>(true);

assertType<IsNullable<string>>(false);
assertType<IsNullable<any>>(false);
assertType<IsNullable<never>>(false);