Skip to main content

fromNullable

@ts-rust/std


@ts-rust/std / Option / fromNullable

Function: fromNullable()

function fromNullable<T>(value): Option<NonNullable<T>>;

Defined in: packages/std/src/option/option.ts:89

Creates an Option from a value that may be null or undefined.

Returns Some if the value is present, or None if it is null or undefined.

Type Parameters

Type ParameterDescription

T

The type of the input value.

Parameters

ParameterTypeDescription

value

T

A value that may be null or undefined.

Returns

Option<NonNullable<T>>

Some with the non-nullable value, or None.

Example

const x = fromNullable(42);
const y = fromNullable<number | null>(null);
const z = fromNullable<string | undefined>(undefined);

expect(x).toStrictEqual(some(42));
expect(y.isNone()).toBe(true);
expect(z.isNone()).toBe(true);