Rust's Result and Option types, for TypeScript.
This library brings Rust's Result and Option enums and their chainable methods to TypeScript with full type safety. See the results-ts API reference for complete method documentation. The API follows Rust's Result and Option types closely.
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.
