Skip to content

Instantly share code, notes, and snippets.

@jancassio
Created February 27, 2023 01:29
Show Gist options
  • Save jancassio/abbaf6b6f8cfcc8f869ff70c4c8d92b4 to your computer and use it in GitHub Desktop.
Save jancassio/abbaf6b6f8cfcc8f869ff70c4c8d92b4 to your computer and use it in GitHub Desktop.
Maybe type + helpers for TypeScript

Maybe type for TypeScript

Maybe is a type to help to work with nullable or undefined types more pleasurable. Very inspired by Haskell and some bits of Rust's pattern matching.

API

Types

Maybe<T>

The Maybe type describes a nullable or undefined for a generic type T. Maybe types can be built using helper functions.

MaybeType

An enum type carrying the two kinds of Maybe values.

Nada

Describes data as null or undefined.

Some<T>

Describes the data has some value (means data != (undefined || null)).

Helpers

maybe

Creates a new Maybe<T> (see examples below).

ensure

The ensure function guarantee to return a value (see examples below).

isSome & isNada

Respectively validates if a Maybe<T> is of Some type or Nada type.

Usage

// Describing a type that hosts some Maybe<T> props
type DataX = {
  a: string;
  b: number;
  c?: string;
  d: Maybe<string>;
  e: Maybe<string>;
};

// A new data of DataX
const data: DataX = {
  a: "1",
  b: 2,
  d: Some("xpto"),    // Should be Some or Nada
  e: Nada(),          // Should be Some or Nada
};

data.a; // '1'
data.b; // 2
data.c; // undefined
data.d; // { type: '__maybe__some__', value: 'xpto' }
data.e; // { type: '__maybe__nada__' }

// creating a new maybe type
const c = maybe(data.c);

// displaying some values using the `ensure` function.
ensure(c, "nothing defined in c");     // 'nothing defined in c'
ensure(data.d, "nothing defined");  // 'xpto'
ensure(data.e, "nothing defined in e");  // 'nothing defined in e'

// checking if a maybe is some or nada
isSome(data.d) // true
isNada(data.d) // false

isSome(data.e) // false
isNada(data.e) // true

License

MIT

/*
maybe.ts
MIT License
Copyright (c) 2023 Jan Cassio
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
const enum MaybeType {
Nada = "__maybe__nada__",
Some = "__maybe__some__",
}
interface Nada {
type: MaybeType.Nada;
}
interface Some<T> {
type: MaybeType.Some;
value: T;
}
export type Maybe<T> = Nada | Some<T>;
export const Nada = (): Nada => ({
type: MaybeType.Nada,
});
export const Some = <T>(value: T): Some<T> => ({
type: MaybeType.Some,
value,
});
const maybe = <T>(optional?: T): Maybe<T> => {
return !!optional ? Some(optional) : Nada();
};
const ensure = <T>(maybe: Maybe<T>, fallback: T): T => {
switch (maybe.type) {
case MaybeType.Nada:
return fallback;
case MaybeType.Some:
return maybe.value;
}
};
const isSome = <T>(left: Maybe<T>): boolean => left.type === MaybeType.Some;
const isNada = <T>(left: Maybe<T>): boolean => left.type === MaybeType.Nada;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment