results-ts
v4.2.6
Published
Rust's Result and Option types, for TypeScript
Downloads
6,776
Maintainers
Readme
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.
Installation
npm install results-ts
# or
bun add results-ts
# or
pnpm add results-ts
# or
deno add results-ts
# or
yarn add results-tsUsage
A 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);Documentation
Full documentation and complete API reference: results-ts.madkarma.top.
Contributing
Interested in contributing? See CONTRIBUTING.md for setup instructions, development guidelines, and pull request expectations.
Attribution
- Web Dev Simplified - concept from this video.
- vultix/ts-results
- supermacro/neverthrow
Contributors
Thanks to all the contributors who helped make this project better!
License
This project is licensed under the MIT License.
