Getting Started
Install @carbonteq/fp and build your first typed pipelines with Result and Option.
Install
$ pnpm add @carbonteq/fp
Repository
Quick Start
Result — Operations that can fail
Use Result<T, E> when an operation can succeed with Ok(T) or fail with Err(E). The error type is explicit in the signature.
import { } from "@carbonteq/fp";
type = { : string; : number };
type = { : string; : string };
function (: <string, string>): <, > {
const = .;
if (!) return .({ : "HOST", : "required" });
const = (.);
if (!.() || < 1 || > 65535) {
return .({ : "PORT", : "must be 1-65535" });
}
return .({ , });
}
const config = ({ : "localhost", : "3000" });
// Transform the success value
const = .(() => `http://${.}:${.}`);
// Provide a fallback
const = .({ : "127.0.0.1", : 8080 });
// Handle both cases
const = .({
: () => `Server at ${.}:${.}`,
: () => `Config error in ${.}: ${.}`,
});Option — Values that may not exist
Use Option<T> when a value may or may not exist. Replaces null/undefined with explicit types.
import { } from "@carbonteq/fp";
type = { : string; : string; : boolean };
declare function (: string): | null;
function (: string): <string> {
return .(())
.(() => .)
.(() => .);
}
const email = ("user-123");
// Provide a default
const = .("no verified email");
// Convert to Result for error context
const = .("User not found or not verified");Chaining Operations
Build pipelines by chaining operators. Each step stays on the "happy path" until an error occurs.
import { } from "@carbonteq/fp";
type = { : string; : number };
type = { : string; : number };
declare function (: string): <string, string>;
declare function (: string, : number): <number, string>;
function (: ): <, string> {
return .()
.(() => (.).(() => ))
.(() => (., .).(() => ({ ..., })))
.(() => ({
: `ORD-${.()}`,
: .,
}))
.(() => .(`Created order ${.}`))
.(() => .(`Order failed: ${}`));
}Pattern Matching
Use matchRes and matchOpt for exhaustive handling of both cases:
import { , , , } from "@carbonteq/fp";
declare function (): <{ : string[] }, Error>;
declare function (): <string[]>;
// Match on Result
const = ();
const = (, {
: () => .,
: () => {
.(.);
return [];
},
});
// Match on Option
const = ();
const = (, {
: () => ,
: () => [],
});Safe Exception Handling
Wrap code that throws with tryCatch (sync) or tryAsyncCatch (async):
import { } from "@carbonteq/fp";
// Sync: wrap JSON.parse
const = (: string) =>
.(
() => .() as unknown,
() => ( instanceof ? : new ("Parse failed")),
);
const parsed = ('{"ok": true}');
// Async: wrap fetch
async function (: string) {
return .(
async () => {
const = await (`/api/users/${}`);
if (!.) throw new (`HTTP ${.}`);
return .() as <{ : string }>;
},
() => ( instanceof ? : new ("Fetch failed")),
).();
}Next Steps
- Building Pipelines — Compose multi-step flows
- Async Handling — Mix sync and async operations
- API Cheatsheet — Quick reference for all operators