gs

@gs-ts/native-monad (0.0.6)

Published 2026-03-30 09:16:11 +02:00 by andreas.bertsch in gs/native-monad

Installation

@gs-ts:registry=
npm install @gs-ts/native-monad@0.0.6
"@gs-ts/native-monad": "0.0.6"

About this package

native-monad

TypeScript monad library using native JS/TS idioms - Result, Option, and Query types.

Install

npm install native-monad

Types

Result<T, E>

Success/failure type. Variants: Ok<T> | Err<E>.

import { ok, err, Result } from 'native-monad';

const success = ok(42);        // Ok<number>
const failure = err('oops');   // Err<string>

success.map(x => x * 2);      // Ok(84)
failure.map(x => x * 2);      // Err('oops')

success.match(
  value => `got ${value}`,
  error => `failed: ${error}`,
); // 'got 42'

// Collection operations
Result.all([ok(1), ok(2), ok(3)]);  // Ok([1, 2, 3])
Result.all([ok(1), err('x')]);      // Err('x')

Option<T>

Presence/absence type. Variants: Some<T> | None.

import { some, none, Option } from 'native-monad';

const present = some('hello');  // Some<string>
const absent = none();          // None

present.map(s => s.toUpperCase());  // Some('HELLO')
absent.map(s => s.toUpperCase());   // None

// Create from nullable values (only null/undefined are falsy)
Option.from(0);         // Some(0)
Option.from(null);      // None
Option.from(undefined); // None

Query<T, E>

Three-state monad combining Result and Option. Variants: OkSome<T> | OkNone | ErrNone<E>.

import { okSome, okNone, errNone } from 'native-monad';

const found = okSome(42);        // success with value
const notFound = okNone();       // success without value
const failed = errNone('error'); // failure

Development

pnpm install
pnpm build        # Build (dual ESM/CJS)
pnpm test         # Run Jest tests
pnpm test:types   # Run vitest type tests
pnpm lint         # ESLint
pnpm typecheck    # tsc --noEmit

Dependencies

Development Dependencies

ID Version
@rolldown/binding-linux-x64-gnu 1.0.0-rc.12
@types/node ^22.0.0
eslint ^9.0.0
prettier ^3.0.0
tsup ^8.0.0
typescript ^5.7.0
typescript-eslint ^8.0.0
vitest ^3.2.4

Keywords

monad result option typescript functional
Details
npm
2026-03-30 09:16:11 +02:00
13
MIT
latest
50 KiB
Assets (1)
Versions (2) View all
0.0.6 2026-03-30
0.0.5 2026-03-30