Skip to main content

fromUndefined

@ts-rust/std


@ts-rust/std / Option / fromUndefined

Function: fromUndefined()

function fromUndefined<T>(value): Option<Exclude<T, undefined>>;

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

Creates an Option from a value that may be undefined.

Returns Some if the value is defined, or None if it is undefined. Unlike fromNullable, null is treated as a present value.

Type Parameters

Type ParameterDescription

T

The type of the defined value. undefined on the argument does not become T; pass an explicit T (or T | undefined) when the value has been narrowed to undefined.

Parameters

ParameterTypeDescription

value

T | undefined

A value that may be undefined.

Returns

Option<Exclude<T, undefined>>

Some with the defined value, or None.

Example

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

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