Skip to main content
Home
This package has been archived, and as such it is read-only.

Built and signed on GitHub Actions

latest

A small, focused TypeScript library for safer code through functional patterns using natural language. Inspired by folktale.

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
a month ago (0.5.0)
class Maybe

Represents an optional value that may or may not exist.

Maybe is a type-safe way to handle nullable values without using null or undefined directly. A Maybe is either Just (contains a value) or Nothing (empty).

This pattern eliminates null errors by forcing you to explicitly handle the absence of a value at compile time.

Examples

Example 1

// Basic usage
function findUser(id: string): Maybe<User> {
  const user = users.get(id)
  return Maybe.FromNullable(user)
}

const result = findUser('123')
result.matchWith({
  Just: (user) => console.log(`Found: ${user.name}`),
  Nothing: () => console.log('User not found')
})

Example 2

// Chaining operations
const userName = findUser(id)
  .map(user => user.profile)
  .map(profile => profile.name)
  .getOrElse('Anonymous')

Example 3

// Creating Maybe values
const just = Maybe.Just(42)
const nothing = Maybe.Nothing<number>()
const fromNullable = Maybe.FromNullable(possiblyNull)

Constructors

new
Maybe(value?: Nullable<Type>)

Type Parameters

Type
  • The type of the value when it exists (Just case)

Methods

chain<HandlerType>(handler: (value: Type) => Maybe<HandlerType>): Maybe<HandlerType>

Transforms the Just value using a function that returns a Maybe (also known as flatMap).

Similar to map, but the handler function returns a Maybe instead of a plain value. This is useful for chaining operations that may themselves return Nothing, avoiding nested Maybes.

getOrElse(defaultValue: NonNullable<Type>): NonNullable<Type>

Gets the Just value or returns a default value if this is Nothing.

This provides a simple way to extract a value from a Maybe with a fallback, without needing to use pattern matching.

Extracts the value from a Just or throws an error if it's Nothing.

Warning: This method throws exceptions and should be used sparingly. Prefer getOrElse or matchWith for safer error handling.

Checks if this Maybe contains a value.

Checks if this Maybe is empty (has no value).

map<HandlerType>(handler: (value: Type) => NonNullable<HandlerType>): Maybe<HandlerType>

Transforms the Just value using the provided function.

If the Maybe is Just, applies the handler to the value and returns a new Just Maybe. If the Maybe is Nothing, returns Nothing without calling the handler.

This allows you to chain transformations on present values while automatically propagating Nothing.

matchWith<
JustReturnType,
NothingReturnType,
>
(pattern: { Just: (value: NonNullable<Type>) => JustReturnType; Nothing: () => NothingReturnType; }): JustReturnType | NothingReturnType

Pattern matches on the Maybe, executing different handlers for Just and Nothing cases.

This is the primary way to extract values from a Maybe. Both cases must be handled, ensuring you never forget to handle the absence of a value.

orElse<HandlerType>(handler: () => Maybe<HandlerType>): Maybe<Type> | Maybe<HandlerType>

Returns this Maybe if it's Just, otherwise calls the handler to provide an alternative.

This allows you to provide an alternative Maybe when this one is Nothing, useful for trying fallback operations or providing computed defaults.

Static Methods

Converts a nullable value into a Maybe.

This is the primary way to create Maybe values from existing code that uses null or undefined. If the value is null or undefined, returns Nothing. Otherwise, returns Just with the value.

HasInstance<Type>(value: unknown): value is Maybe<Type>

Type guard to check if a value is a Maybe instance.

Just<Type>(value: NonNullable<Type>): Maybe<Type>

Creates a Maybe containing a value.

Use this when you have a value that definitely exists and want to wrap it in a Maybe for use with other Maybe-returning functions.

Nothing<Type>(): Maybe<Type>

Creates an empty Maybe representing the absence of a value.

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@folklore/folklore

Import symbol

import { Maybe } from "@folklore/folklore";
or

Import directly with a jsr specifier

import { Maybe } from "jsr:@folklore/folklore";

Add Package

pnpm i jsr:@folklore/folklore
or (using pnpm 10.8 or older)
pnpm dlx jsr add @folklore/folklore

Import symbol

import { Maybe } from "@folklore/folklore";

Add Package

yarn add jsr:@folklore/folklore
or (using Yarn 4.8 or older)
yarn dlx jsr add @folklore/folklore

Import symbol

import { Maybe } from "@folklore/folklore";

Add Package

vlt install jsr:@folklore/folklore

Import symbol

import { Maybe } from "@folklore/folklore";

Add Package

npx jsr add @folklore/folklore

Import symbol

import { Maybe } from "@folklore/folklore";

Add Package

bunx jsr add @folklore/folklore

Import symbol

import { Maybe } from "@folklore/folklore";