Rust's Result and Option types, for TypeScript.
This library mimics the Rust Result and Option enums and includes some of their chainable methods adapted for TypeScript, with full type safety. For full documentation on the usage of the available methods, please refer to the official Rust docs (or read the JSDocs directly in your editor).
npm install results-ts
# or
bun add results-ts
# or
pnpm add results-ts
# or
deno add results-ts
# or
yarn add results-tsA quick taste of the Result type:
import { Ok, Err } from 'results-ts';
const parseUserId = (id: string) => {
const parsed = parseInt(id, 10);
if (isNaN(parsed))
return Err({
code: 'INVALID_INPUT',
message: 'ID must be a valid number'
} as const);
if (parsed <= 0)
return Err({
code: 'INVALID_ID',
message: 'ID must be positive'
} as const);
return Ok(parsed);
};
const message = parseUserId('10')
.map((id) => id + 3)
.andThen((id) => Ok({ id, name: 'Alice', role: 'admin' }))
.match({
Ok: (user) => `Welcome, ${user.role} ${user.name}!`,
Err: (error) => `Validation Error: ${error.message}`
});
console.log(message);Full documentation and complete API reference: results-ts.madkarma.top.
Interested in contributing? See CONTRIBUTING.md for setup instructions, development guidelines, and pull request expectations.
- Web Dev Simplified - concept from this video.
- vultix/ts-results
- supermacro/neverthrow
Thanks to all the contributors who helped make this project better!
This project is licensed under the MIT License.
