{"id":32709574,"url":"https://github.com/brettchalupa/result","last_synced_at":"2026-05-05T18:31:41.119Z","repository":{"id":320114693,"uuid":"1080857863","full_name":"brettchalupa/result","owner":"brettchalupa","description":"Type-safe Result pattern for TypeScript, inspired by Rust","archived":false,"fork":false,"pushed_at":"2025-10-22T11:47:02.000Z","size":27,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-11-02T04:03:15.684Z","etag":null,"topics":["bun","deno","error-handling","functional-programming","jsr","monads","result","rust-inspired","type-safe","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/brettchalupa.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-10-22T01:01:02.000Z","updated_at":"2025-10-26T16:51:31.000Z","dependencies_parsed_at":"2025-10-22T03:13:37.398Z","dependency_job_id":"815399ef-a97a-4c0c-b2b4-832da80cec62","html_url":"https://github.com/brettchalupa/result","commit_stats":null,"previous_names":["brettchalupa/result"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/brettchalupa/result","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brettchalupa%2Fresult","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brettchalupa%2Fresult/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brettchalupa%2Fresult/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brettchalupa%2Fresult/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/brettchalupa","download_url":"https://codeload.github.com/brettchalupa/result/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brettchalupa%2Fresult/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32662848,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-05T11:29:49.557Z","status":"ssl_error","status_checked_at":"2026-05-05T11:29:48.587Z","response_time":54,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bun","deno","error-handling","functional-programming","jsr","monads","result","rust-inspired","type-safe","typescript"],"created_at":"2025-11-02T04:01:08.634Z","updated_at":"2026-05-05T18:31:41.114Z","avatar_url":"https://github.com/brettchalupa.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @brettchalupa/result\n\nA TypeScript library implementing the Result pattern for type-safe error\nhandling, inspired by Rust's `Result\u003cT, E\u003e`.\n\nView the package on JSR: https://jsr.io/@brettchalupa/result\n\n## Why?\n\nTraditional `try/catch` error handling in TypeScript is painful:\n\n- ❌ **Invisible control flow** - Can't tell which functions throw by looking at\n  signatures\n- ❌ **No type safety** - Caught errors are `unknown`, TypeScript can't help you\n- ❌ **Difficult to test** - Easy to miss error paths and leave them untested\n- ❌ **Unclear propagation** - Hard to tell where errors originate when they\n  bubble up\n\n**Result makes errors explicit:**\n\n```typescript\n// ❌ Exception-based: Which functions throw? What error types?\nfunction processUser(id: string): User {\n  const user = findUser(id); // Throws? Maybe?\n  return validateUser(user); // Throws? Who knows?\n}\n\n// ✅ Result-based: Clear from the signature what can fail\nfunction processUser(id: string): Result\u003cUser, \"NOT_FOUND\" | \"INVALID\"\u003e {\n  const user = findUser(id); // Returns Result\u003cUser, \"NOT_FOUND\"\u003e\n  return user.andThen(validateUser); // Type-safe chaining!\n}\n```\n\nWith Result, **errors are just values** - visible in the type system, easy to\nhandle, and impossible to ignore accidentally.\n\n## Features\n\n- **Type-safe error handling** - Errors are part of the function signature\n- **Method chaining** - Fluent API for transforming and combining results\n- **Zero dependencies** - Lightweight and fast\n- **Full TypeScript support** - Complete type inference and safety\n- **Well tested** - Comprehensive test suite with 50+ tests\n\n## Installation\n\n```bash\n# Deno\ndeno add jsr:@brettchalupa/result\n\n# pnpm 10.9+\npnpm add jsr:@brettchalupa/result\n\n# yarn 4.9+\nyarn add jsr:@brettchalupa/result\n\n# npm, bun, and older versions of yarn or pnpm\nnpx jsr add @brettchalupa/result # replace npx with any of yarn dlx, pnpm dlx, or bunx\n```\n\nOr import directly in Deno without installation:\n\n```typescript\nimport { err, ok, Result } from \"jsr:@brettchalupa/result\";\n\nconst result = ok(42);\n```\n\n## Quick Start\n\n```typescript\nimport { err, ok, type Result } from \"@brettchalupa/result\";\n\nfunction divide(a: number, b: number): Result\u003cnumber, string\u003e {\n  if (b === 0) {\n    return err(\"Cannot divide by zero\");\n  }\n  return ok(a / b);\n}\n\nconst result = divide(10, 2);\nif (result.isOk()) {\n  console.log(\"Result:\", result.data); // Result: 5\n} else {\n  console.error(\"Error:\", result.error);\n}\n```\n\n## Usage\n\n### Basic Results\n\nCreate successful or failed results:\n\n```typescript\nimport { err, ok } from \"@brettchalupa/result\";\n\n// Success\nconst success = ok(42);\nconsole.log(success.data); // 42\n\n// Failure\nconst failure = err(\"Something went wrong\");\nconsole.log(failure.error); // \"Something went wrong\"\n```\n\n### Method Chaining\n\nTransform results with a fluent API:\n\n```typescript\nconst result = ok(5)\n  .map((x) =\u003e x * 2)\n  .map((x) =\u003e x.toString()); // ok(\"10\")\n\nconst error = err(\"not_found\").mapErr((code) =\u003e ({\n  code,\n  message: \"Resource not found\",\n}));\n```\n\n### Handling Async Operations\n\n```typescript\nimport { Result } from \"@brettchalupa/result\";\n\n// Wrap throwing code\nconst parsed = Result.try(() =\u003e JSON.parse(jsonString));\n\n// Wrap async operations\nconst data = await Result.tryAsync(() =\u003e fetch(url).then((r) =\u003e r.json()));\n```\n\n### Combining Results\n\n```typescript\nimport { err, ok, Result } from \"@brettchalupa/result\";\n\n// Collect all successes or get first error\nconst results = [ok(1), ok(2), ok(3)];\nconst combined = Result.all(results); // ok([1, 2, 3])\n\n// Partition successes and failures\nconst mixed = [ok(1), err(\"error\"), ok(3)];\nconst [successes, failures] = Result.partition(mixed);\n// successes: [1, 3]\n// failures: [\"error\"]\n```\n\n### Repository Pattern\n\n```typescript\nimport { err, ok, type Result } from \"@brettchalupa/result\";\n\ntype User = { id: string; name: string };\n\n// Define your own domain-specific error types\ntype DbError = \"NOT_FOUND\" | \"CONNECTION_ERROR\" | \"PERMISSION_DENIED\";\n\nasync function findUser(id: string): Promise\u003cResult\u003cUser, DbError\u003e\u003e {\n  try {\n    const user = await db.findOne({ id });\n    if (!user) {\n      return err(\"NOT_FOUND\");\n    }\n    return ok(user);\n  } catch (error) {\n    return err(\"CONNECTION_ERROR\");\n  }\n}\n```\n\n## API Reference\n\n### Constructors\n\n- `ok\u003cT\u003e(data: T): Ok\u003cT\u003e` - Create a successful result\n- `err\u003cE\u003e(error: E): Err\u003cE\u003e` - Create a failed result\n\n### Instance Methods\n\nBoth `Ok` and `Err` types support:\n\n- `.isOk()` - Type guard for success\n- `.isErr()` - Type guard for failure\n- `.map(fn)` - Transform the success value\n- `.mapErr(fn)` - Transform the error value\n- `.andThen(fn)` - Chain operations that return Results\n- `.unwrapOr(defaultValue)` - Get value or default\n- `.unwrap()` - Get value or throw\n- `.expect(message)` - Get value or throw with message\n\n### Utility Functions\n\nThe `Result` namespace provides:\n\n- `Result.try(fn)` - Wrap a throwing function\n- `Result.tryAsync(fn)` - Wrap an async throwing function\n- `Result.all(results)` - Combine results\n- `Result.partition(results)` - Separate successes and failures\n- `Result.collectErrors(results)` - Get all errors\n- `Result.map(result, fn)` - Static map function\n- `Result.mapErr(result, fn)` - Static mapErr function\n- `Result.andThen(result, fn)` - Static andThen function\n- `Result.unwrapOr(result, default)` - Static unwrapOr function\n- `Result.unwrap(result)` - Static unwrap function\n- `Result.expect(result, message)` - Static expect function\n\n## Development\n\n```bash\n# Run tests\ndeno test\n\n# Run all checks (format, lint, type check, test)\ndeno task ok\n\n# Watch mode\ndeno task dev\n```\n\n## License\n\nThis is free and unencumbered software released into the public domain. See\n[UNLICENSE](./UNLICENSE) for details.\n\n## Contributing\n\nContributions are welcome! Please feel free to submit issues or pull requests.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrettchalupa%2Fresult","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrettchalupa%2Fresult","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrettchalupa%2Fresult/lists"}