Handle failure
Return a success or an error. Keep the failure visible in the type and handle both outcomes where they matter.
Read the guideDeesseJS FP
Result and Maybe for TypeScript. Make success, failure and absence part of your types, then compose the next step.
npm install @deessejs/fpimport { ok, err, type Result } from '@deessejs/fp';
function parseAge(input: string): Result<number, string> {
const age = Number(input);
return input.trim() !== '' && Number.isInteger(age) && age >= 0
? ok(age)
: err('Enter a valid age');
}
parseAge('22').match({
ok: (age) => `Age: ${age}`,
err: (error) => error,
});Why FP
Keep the successful path readable, without leaving failures or missing values implicit.
Return a success or an error. Keep the failure visible in the type and handle both outcomes where they matter.
Read the guideRepresent a value that might be missing. Transform it when present and choose a fallback when absent.
Read the guideChain transformations with map and flatMap. Continue on success and preserve the first failure along the way.
Read the guideBefore & after
Compare ordinary TypeScript with FP on a small, complete example. Choose the representation that fits your code.
Both versions handle division by zero. Result carries the reason for failure in its return type.
function divide(a: number, b: number): number | undefined {
if (b === 0) return undefined;
return a / b;
}
const result = divide(10, 0);
const message = result !== undefined
? String(result)
: 'Division by zero';import { ok, err, type Result } from '@deessejs/fp';
function divide(a: number, b: number): Result<number, string> {
return b === 0 ? err('Division by zero') : ok(a / b);
}
const message = divide(10, 0).match({
ok: (value) => String(value),
err: (error) => error,
});The primitives
API referenceStart with Result and Maybe. Use Unit when a function needs an explicit value for an otherwise empty return.
Result<T, E>A success carrying T or a failure carrying E. Transform either branch and handle the outcome with match.
Explore ResultMaybe<T>A present value or none. Map the value, chain another lookup or provide an explicit fallback.
Explore MaybeUnitA single value for an operation with no meaningful return value. It does not manage or isolate side effects.
Explore UnitGet started
Install the package and start with Result or Maybe. Explore the API as you need it.
npm install @deessejs/fp