Documentation
Maybe

@tsly/maybe

This package publishes utilities for working with any nullish values. It's method are designed to be chainable and were inspired by kotlin-style scope functions (opens in a new tab).

The maybe Function

import { maybe } from "@tsly/maybe";

The maybe function is the factory for the underlying Maybe<T> class, however the maybe() function has some important typings.

This package largely relies upon nullish/optional chaining (opens in a new tab). As such, calling maybe will return null for any nullish value, and Maybe<T> for any non-nullish value. This logic is recursive for any chainable method within the Maybe<T> class.

import { maybe } from "@tsly/maybe";
 
const value: string | undefined = // ...
const example = maybe(value);
//    ^? Maybe<string> | null

Ancillary Exports

import { isSome } from "@tsly/maybe";
const arr = ['one', 'two', undefined, 'three', null];
 
console.log(arr.filter(isSome)); // ['one', 'two', 'three'];
import { isNone } from "@tsly/maybe";
const value: string | undefined = // ...
 
if (isNone(value)) {
    return;
}
 
value // <-- now typed as `string`
// idiomatic counterpart to the `NonNullable` builtin
import { type Nullable } from "@tsly/maybe";
 
type Wrapped = Nullable<string>; // string | null
type Unwrapped = NonNullable<MyType>; // string