{"id":15697077,"url":"https://github.com/johannschopplich/resultx","last_synced_at":"2026-03-14T05:37:46.826Z","repository":{"id":168124100,"uuid":"643768268","full_name":"johannschopplich/resultx","owner":"johannschopplich","description":"🤝 Minimalist, strongly-typed result pattern for TypeScript","archived":false,"fork":false,"pushed_at":"2025-03-02T19:20:09.000Z","size":344,"stargazers_count":10,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-07T12:48:40.917Z","etag":null,"topics":["result-pattern","result-type","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/johannschopplich.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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}},"created_at":"2023-05-22T05:52:07.000Z","updated_at":"2025-03-04T18:53:05.000Z","dependencies_parsed_at":null,"dependency_job_id":"a87783e6-8c06-4601-9a4c-f84f171051e7","html_url":"https://github.com/johannschopplich/resultx","commit_stats":null,"previous_names":["johannschopplich/unres"],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johannschopplich%2Fresultx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johannschopplich%2Fresultx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johannschopplich%2Fresultx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johannschopplich%2Fresultx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/johannschopplich","download_url":"https://codeload.github.com/johannschopplich/resultx/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252961835,"owners_count":21832190,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["result-pattern","result-type","typescript"],"created_at":"2024-10-03T19:11:37.584Z","updated_at":"2025-10-03T22:34:16.122Z","avatar_url":"https://github.com/johannschopplich.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# resultx\n\nA lightweight and simple `Result` type for TypeScript, inspired by Rust's Result type.\n\n## Description\n\n`resultx` provides a `Result` type that represents either success (`Ok`) or failure (`Err`). It helps to handle errors in a more explicit and type-safe way, without relying on exceptions.\n\nFor error handling in synchronous code, `resultx` provides a `trySafe` function that wraps a function that might throw an error. For asynchronous code, `trySafe` can also be used with promises.\n\n## Key Features\n\n- 🎭  Simple and intuitive `Result` type, wrapping `Ok` and `Err` values\n- 🚀 Supports both synchronous and asynchronous operations\n- 🛡️ Type-safe error handling\n- 🧰 Zero dependencies\n- 📦 Tiny bundle size (half a kilobyte minified)\n\n## Table of Contents\n\n- [Installation](#installation)\n- [Usage](#usage)\n- [API](#api)\n- [Examples](#examples)\n\n## Installation\n\nAdd `resultx` to your dependencies by running one of the following commands, depending on your package manager:\n\n```bash\npnpm add -D resultx\n\n# Or with npm\nnpm install -D resultx\n\n# Or with yarn\nyarn add -D resultx\n```\n\n## Usage\n\n```ts\nimport { err, ok, trySafe, unwrap } from 'resultx'\n\n// Create `Ok` and `Err` results\nconst successResult = ok(42)\n//                    ^? Ok\u003cnumber\u003e\nconst failureResult = err('Something went wrong')\n//                    ^? Err\u003c\"Something went wrong\"\u003e\n\n// Use `trySafe` for error handling\nconst result = trySafe(() =\u003e {\n  // Your code that might throw an error\n  return JSON.parse('{\"foo\":\"bar\"}')\n})\n\n// Either log the result or the error\nif (result.ok) {\n  console.log('Parsed JSON:', result.value)\n}\nelse {\n  console.error('Failed to parse JSON:', result.error)\n}\n\n// Or unwrap and destructure the result\nconst { value, error } = unwrap(result)\n```\n\n## API\n\n### `Result`\n\nThe `Result` type represents either success (`Ok`) or failure (`Err`).\n\n**Type Definition:**\n\n```ts\ntype Result\u003cT, E\u003e = Ok\u003cT\u003e | Err\u003cE\u003e\n```\n\n#### `Ok`\n\nThe `Ok` type wraps a successful value.\n\n**Example:**\n\n```ts\nconst result = new Ok(42)\n```\n\n**Type Definition:**\n\n```ts\ndeclare class Ok\u003cT\u003e {\n  readonly value: T\n  readonly ok: true\n  constructor(value: T)\n}\n```\n\n### `Err`\n\nThe `Err` type wraps an error value.\n\n**Example:**\n\n```ts\nconst result = new Err('Something went wrong')\n```\n\n**Type Definition:**\n\n```ts\ndeclare class Err\u003cE\u003e {\n  readonly error: E\n  readonly ok: false\n  constructor(error: E)\n}\n```\n\n### `ok`\n\nShorthand function to create an `Ok` result. Use it to wrap a successful value.\n\n**Type Definition:**\n\n```ts\nfunction ok\u003cT\u003e(value: T): Ok\u003cT\u003e\n```\n\n### `err`\n\nShorthand function to create an `Err` result. Use it to wrap an error value.\n\n**Type Definition:**\n\n```ts\nfunction err\u003cE extends string = string\u003e(err: E): Err\u003cE\u003e\nfunction err\u003cE = unknown\u003e(err: E): Err\u003cE\u003e\n```\n\n### `trySafe`\n\nWraps a function that might throw an error and returns a `Result` with the result of the function.\n\n**Type Definition:**\n\n```ts\nfunction trySafe\u003cT, E = unknown\u003e(fn: () =\u003e T): Result\u003cT, E\u003e\nfunction trySafe\u003cT, E = unknown\u003e(promise: Promise\u003cT\u003e): Promise\u003cResult\u003cT, E\u003e\u003e\n```\n\n### `unwrap`\n\nUnwraps a `Result`, `Ok`, or `Err` value and returns the value or error in an object. If the result is an `Ok`, the object contains the value and an `undefined` error. If the result is an `Err`, the object contains an `undefined` value and the error.\n\n**Example:**\n\n```ts\nconst result = trySafe(() =\u003e JSON.parse('{\"foo\":\"bar\"}'))\nconst { value, error } = unwrap(result)\n```\n\n**Type Definition:**\n\n```ts\nfunction unwrap\u003cT\u003e(result: Ok\u003cT\u003e): { value: T, error: undefined }\nfunction unwrap\u003cE\u003e(result: Err\u003cE\u003e): { value: undefined, error: E }\nfunction unwrap\u003cT, E\u003e(result: Result\u003cT, E\u003e): { value: T, error: undefined } | { value: undefined, error: E }\n```\n\n## Examples\n\n### Basic Usage\n\nA common use case for `Result` is error handling in functions that might fail. Here's an example of a function that divides two numbers and returns a `Result`:\n\n```ts\nimport { err, ok } from 'resultx'\n\nfunction divide(a: number, b: number) {\n  if (b === 0) {\n    return err('Division by zero')\n  }\n  return ok(a / b)\n}\n\nconst result = divide(10, 2)\nif (result.ok) {\n  console.log('Result:', result.value)\n}\nelse {\n  console.error('Error:', result.error)\n}\n```\n\n### Error Handling with `trySafe`\n\nThe `trySafe` function is useful for error handling in synchronous code. It wraps a function that might throw an error and returns a `Result`:\n\n```ts\nimport { trySafe } from 'resultx'\n\nconst result = trySafe(() =\u003e JSON.parse('{\"foo\":\"bar\"}'))\n\nif (result.ok) {\n  console.log('Parsed JSON:', result.value)\n}\nelse {\n  console.error('Failed to parse JSON:', result.error)\n}\n```\n\n### Async Operations with `trySafe`\n\nFor asynchronous operations, `trySafe` can also be used with promises. Here's an example of fetching data from an API:\n\n```ts\nimport { trySafe } from 'resultx'\n\nasync function fetchData() {\n  const result = await trySafe(fetch('https://api.example.com/data'))\n\n  if (result.ok) {\n    const data = await result.value.json()\n    console.log('Fetched data:', data)\n  }\n  else {\n    console.error('Failed to fetch data:', result.error)\n  }\n}\n\nfetchData()\n```\n\n## License\n\n[MIT](./LICENSE) License © 2023-PRESENT [Johann Schopplich](https://github.com/johannschopplich)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohannschopplich%2Fresultx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjohannschopplich%2Fresultx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohannschopplich%2Fresultx/lists"}