{"id":16473192,"url":"https://github.com/dscheglov/resultage","last_synced_at":"2025-03-23T11:32:49.922Z","repository":{"id":214072190,"uuid":"734725861","full_name":"DScheglov/resultage","owner":"DScheglov","description":"A clear way for handling success and failure in both synchronous and asynchronous operations.","archived":false,"fork":false,"pushed_at":"2025-03-01T12:41:02.000Z","size":196,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-20T04:09:15.933Z","etag":null,"topics":["either","err","error","error-as-values","error-handling","functor","monad","never-throw","ok","result","throw"],"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/DScheglov.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-12-22T12:52:10.000Z","updated_at":"2025-03-01T12:41:06.000Z","dependencies_parsed_at":"2023-12-25T17:40:52.460Z","dependency_job_id":"595d7957-f661-489d-bb88-3da692605cf1","html_url":"https://github.com/DScheglov/resultage","commit_stats":null,"previous_names":["dscheglov/ts-result","dscheglov/result-ts","dscheglov/okerr-ts","dscheglov/resultage"],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DScheglov%2Fresultage","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DScheglov%2Fresultage/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DScheglov%2Fresultage/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DScheglov%2Fresultage/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DScheglov","download_url":"https://codeload.github.com/DScheglov/resultage/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245097158,"owners_count":20560311,"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":["either","err","error","error-as-values","error-handling","functor","monad","never-throw","ok","result","throw"],"created_at":"2024-10-11T12:25:42.941Z","updated_at":"2025-03-23T11:32:49.628Z","avatar_url":"https://github.com/DScheglov.png","language":"TypeScript","readme":"# resultage [![Coverage Status](https://coveralls.io/repos/github/DScheglov/resultage/badge.svg?branch=main)](https://coveralls.io/github/DScheglov/resultage?branch=main) [![npm version](https://img.shields.io/npm/v/resultage.svg?style=flat-square)](https://www.npmjs.com/package/resultage) [![npm downloads](https://img.shields.io/npm/dm/resultage.svg?style=flat-square)](https://www.npmjs.com/package/resultage) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/DScheglov/resultage/blob/master/LICENSE)\n\nA clear way for handling success and failure in both synchronous and asynchronous operations.\n\n## Installation\n\n```bash\nnpm install resultage\n```\n\n## Usage\n\n### Creating a Result\n\n```typescript\nimport { Result, ok, err } from 'resultage';\n\ntype JsonObject = Record\u003cstring, unknown\u003e;\n\nconst okIfObject = (value: unknown): Result\u003cJsonObject, 'ERR_NOT_AN_OBJECT'\u003e =\u003e\n  typeof value === 'object' \u0026\u0026 value !== null \u0026\u0026 !Array.isArray(value)\n    ? ok(value as JsonObject)\n    : err('ERR_NOT_AN_OBJECT');\n\nconst okIfInt = (value: unknown): Result\u003cnumber, 'ERR_NOT_AN_INT'\u003e =\u003e\n  Number.isInteger(value)\n    ? ok(value as number)\n    : err('ERR_NOT_AN_INT');\n\nconst okIfString = (value: unknown): Result\u003cstring, 'ERR_NOT_A_STRING'\u003e =\u003e\n  typeof value === 'string'\n    ? ok(value)\n    : err('ERR_NOT_A_STRING');\n```\n\n### Composing with Do-notation\n\n```typescript\ntype Person = {\n  name: string;\n  age: number;\n}\n\nconst okIfPerson = (value: unknown): Result\u003cPerson, 'ERR_NOT_A_PERSON'\u003e =\u003e\n  Do(function*() {\n    const obj = yield* okIfObject(value);\n    const name = yield* okIfString(obj.name);\n    const age = yield* okIfInt(obj.age);\n\n    return { name, age };\n  }).mapErr(() =\u003e 'ERR_NOT_A_PERSON');\n\nconst person: Person = okIfPerson({ name: 'John', age: 42 }).unwrap();\n```\n\n### Composing with chain\n\n```typescript\nconst okIfPerson =\n  (value: unknown) =\u003e okIfObject(value).chain(\n  (obj)            =\u003e okIfString(obj.name).chain(\n  (name)           =\u003e okIfInt(obj.age).chain(\n  (age)            =\u003e ok({ name, age })\n)));\n```\n\nor the same with `map` on the last step:\n\n```typescript\nconst okIfPerson =\n  (value: unknown) =\u003e okIfObject(value).chain(\n  (obj)            =\u003e okIfString(obj.name).chain(\n  (name)           =\u003e okIfInt(obj.age).map(\n  (age)            =\u003e ({ name, age })\n)));\n```\n\n\u003e Note: from the performance perspective, using `chain` is preferable to `Do`-notation,\n\u003e because `chain` doesn't create and run generators.\n\u003e However, `Do`-notation is more readable and easier to use.\n\u003e Additionally, the formatting of the code in this section requires specific\n\u003e linters and formatters configuration.\n\n### Collecting Ok-s from a Result Array\n\n```typescript\nconst lordOfTheRingsAuthors = collect([\n  ok({ id, name: 'J. R. R. Tolkien' }),\n  ok({ id, name: 'Christopher Tolkien' }),\n]);\n\nconst silmarillionAuthors = collect([\n  ok({ id, name: 'J. R. R. Tolkien' }),\n  err('ERR_PERSON_NOT_FOUND' as const),\n]);\n\nconsole.log(lordOfTheRingsAuthors.unwrap());\n// Prints to console:\n// [\n//   { id, name: 'J. R. R. Tolkien' },\n//   { id, name: 'Christopher Tolkien' }\n// ]\n\nconsole.log(silmarillionAuthors.unwrapErr());\n// Prints to console: ERR_PERSON_NOT_FOUND\n```\n\n### Working with Async Results\n\n```typescript\nimport { Do, collect, err, ok } from 'resultage';\n\nconst getBookWithAuthors = (bookId: string) =\u003e\n  Do(async function* () {\n    const book = yield* await fetchBook(bookId);\n    const authors = yield* await fetchPersons(book.authorIds);\n\n    return { ...book, authors };\n  });\n\nconst fetchBook = async (id: string) =\u003e (\n  id === '1' ? ok({ id, title: 'The Lord of the Rings', authorIds: ['1', '2'] }) :\n  id === '2' ? ok({ id, title: 'The Silmarillion', authorIds: ['1', '3'] }) :\n  err('ERR_BOOK_NOT_FOUND' as const)\n);\n\nconst fetchPersons = async (ids: string[]) =\u003e collect(\n  ids.map(id =\u003e (\n    id === '1' ? ok({ id, name: 'J. R. R. Tolkien' }) :\n    id === '2' ? ok({ id, name: 'Christopher Tolkien' }) :\n    err(\"ERR_PERSON_NOT_FOUND\" as const)\n  ))\n);\n\nasync function run() {\n  const LordOfTheRings = await getBookWithAuthors('1');\n  console.log(LordOfTheRings.unwrap());\n  // Prints to console book with authors populated\n\n  const Silmarillion = await getBookWithAuthors('2');\n  console.log(Silmarillion.unwrapErr());\n  // Prints to console: ERR_PERSON_NOT_FOUND\n\n  const TheHobbit = await getBookWithAuthors('3');\n  console.log(TheHobbit.unwrapErr());\n  // Prints to console: ERR_BOOK_NOT_FOUND\n}\n\nrun().catch(console.error);\n```\n\n## Documentation\n\n[TODO: insert link to documentation]\n\n## Result Type\n\n`Result\u003cT, E\u003e` is a generic type that represents either success or failure, and\nis an union of `Ok\u003cT\u003e` and `Err\u003cE\u003e` types:\n  \n  ```typescript\n  type Result\u003cT, E\u003e = Ok\u003cT\u003e | Err\u003cE\u003e;\n  ```\n\nWhere:\n\n- `Ok\u003cT\u003e` is a type that represents success and wraps the value of type `T`.\n- `Err\u003cE\u003e` is a type that represents failure and wraps the error of type `E`.\n\n### `Ok\u003cT\u003e` Interface\n\n`Ok\u003cT\u003e` is an interface that extends the `ResultInterface\u003cT, never\u003e` interface\nwith the following structure.\n\n```typescript\ninterface Ok\u003cT\u003e extends ResultInterface\u003cT, never\u003e {\n  readonly value: T;\n  readonly isOk: true;\n  readonly isErr: false;\n}\n```\n\nThe property `value` is accessible only when the type of the correspondent variable\nor parameter is narrowed from the `Result\u003cT, E\u003e` to the `Ok\u003cT\u003e`.\n\nTo narrow the type of the variable or parameter to `Ok\u003cT\u003e`, use either the `isOk` method\nor the `isErr` method on the `Result\u003cT, E\u003e` instance.\n\n**Note**: The `Ok\u003cT\u003e` is an interface, not a class, so it is not possible to create\nan instance of `Ok\u003cT\u003e` directly. Use the `ok` function to create an instance of `Ok\u003cT\u003e`.\n\n### `Err\u003cE\u003e` Interface\n\n`Err\u003cE\u003e` is an interface that extends the `ResultInterface\u003cnever, E\u003e` interface\nwith the following structure.\n\n```typescript\ninterface Err\u003cE\u003e extends ResultInterface\u003cnever, E\u003e {\n  readonly error: E;\n  readonly isOk: false;\n  readonly isErr: true;\n}\n```\n\nThe property `error` is accessible only when the type of the correspondent variable\nor parameter is narrowed from the `Result\u003cT, E\u003e` to the `Err\u003cE\u003e`.\n\nTo narrow the type of the variable or parameter to `Err\u003cE\u003e`, use either the `isOk` method\nor the `isErr` method on the `Result\u003cT, E\u003e` instance.\n\n**Note**: The `Err\u003cE\u003e` is an interface, not a class, so it is not possible to create\nan instance of `Err\u003cE\u003e` directly. Use the `err` function to create an instance of `Err\u003cE\u003e`.\n\n### `ResultInterface\u003cT, E\u003e` Interface\n\n`ResultInterface\u003cT, E\u003e` is an interface that defines the common `Result` methods.\n\n```typescript\ninterface ResultInterface\u003cT, E\u003e {\n  map\u003cS\u003e(fn: (data: T) =\u003e S): Result\u003cS, E\u003e;\n  mapErr\u003cF\u003e(fn: (error: E) =\u003e F): Result\u003cT, F\u003e;\n  chain\u003cS, F\u003e(next: (data: T) =\u003e Result\u003cS, F\u003e): Result\u003cS, F | E\u003e;\n  chainErr\u003cS, F\u003e(next: (error: E) =\u003e Result\u003cS, F\u003e): Result\u003cT | S, F\u003e;\n  unwrap(): T;\n  unwrapOr\u003cS\u003e(fallback: S): T | S;\n  unwrapOrElse\u003cS\u003e(fallback: (error: E) =\u003e S): T | S;\n  unwrapErr(): E;\n  unwrapErrOr\u003cF\u003e(fallback: F): E | F;\n  unwrapErrOrElse\u003cF\u003e(fallback: (data: T) =\u003e F): E | F;\n  unwrapOrThrow(): T;\n  unpack(): T | E;\n  match\u003cER, TR\u003e(\n    okMatcher: (data: T) =\u003e TR,\n    errMatcher: (error: E) =\u003e ER,\n  ): ER | TR;\n  tap(fn: (data: T) =\u003e void): Result\u003cT, E\u003e;\n  tapErr(fn: (error: E) =\u003e void): Result\u003cT, E\u003e;\n  biMap\u003cS, F\u003e(okFn: (data: T) =\u003e S, errFn: (error: E) =\u003e F): Result\u003cS, F\u003e;\n  biChain\u003cTS, TF, ES, EF\u003e(\n    okFn: (data: T) =\u003e Result\u003cTS, TF\u003e,\n    errFn: (error: E) =\u003e Result\u003cES, EF\u003e,\n  ): Result\u003cTS | ES, TF | EF\u003e;\n  [Symbol.iterator](): Generator\u003cE, T\u003e;\n}\n```\n\n## Constructors\n\nAs mentioned above, `Ok\u003cT\u003e` and `Err\u003cE\u003e` are interfaces, not classes, so it is not\npossible to create an instance of `Ok\u003cT\u003e` or `Err\u003cE\u003e` directly. Use the following\nfunctions to create an instance of `Ok\u003cT\u003e` or `Err\u003cE\u003e`.\n\n### Function ok(value)\n\nCreates an instance of `OkImpl\u003cT\u003e` class (that is not exported from the package).\n\nFunction Signature:\n\n```typescript\nconst ok: \u003cT\u003e(value: T) =\u003e Ok\u003cT\u003e\n```\n\nExample:\n\n```typescript\nimport { ok } from 'resultage';\n\nconst okNumber = ok(42);\n```\n\n### Function err(error)\n\nCreates an instance of `ErrImpl\u003cE\u003e` class (that is not exported from the package).\n\nFunction Signature:\n\n```typescript\nconst err: \u003cE\u003e(error: E) =\u003e Err\u003cE\u003e\n```\n\nExample:\n\n```typescript\nimport { err } from 'resultage';\n\nconst errString = err('Error message');\n```\n\n## Properties And Methods of `Result\u003cT, E\u003e`\n\n### Property .isOk: boolean\n\nReturns `true` if Result is `Ok\u003cT\u003e`, `false` otherwise. Narrows the `Result\u003cT, E\u003e` to `Ok\u003cT\u003e` in \"if\"-branches,\nand to `Err\u003cE\u003e` in \"else\"-branches.\n\nProperty Definition:\n\n```typescript\ninterface Ok\u003cT\u003e { readonly isOk: true }\ninterface Err\u003cE\u003e { readonly isOk: false } \n```\n\nFunction Signature:\n\n```typescript\nconst isOk: \u003cT, E\u003e(result: Result\u003cT, E\u003e) =\u003e result is Ok\u003cT\u003e\n```\n\nExample:\n\n```typescript\nimport { ok } from 'resultage';\n\nconst result = ok(42);\n\nif (result.isOk) {\n  console.log(result.value);\n} else {\n  console.error(result.error);\n}\n```\n\nExample with function:\n\n```typescript\nimport { ok, isOk } from 'resultage';\n\nconst result = ok(42);\n\nif (isOk(result)) {\n  console.log(result.value);\n} else {\n  console.error(result.error);\n}\n```\n\nThe function `isOk(result)` is good to be used as a callback in\nthe `Array.prototype.filter` method or similar.\n\n```typescript\nimport { isOk } from 'resultage';\n\nconst results = [ok(42), err('Error')];\n\nconst isEverythingOk = results.every(isOk);\n```\n\n### Property .isErr: boolean\n\nReturns `true` if Result is `Err\u003cE\u003e`, `false` otherwise. Narrows the `Result\u003cT, E\u003e` to `Err\u003cE\u003e` in \"if\"-branches,\nand to `Ok\u003cT\u003e` in \"else\"-branches.\n\nProperty Definition:\n\n```typescript\ninterface Ok\u003cT\u003e { readonly isErr: false }\ninterface Err\u003cE\u003e { readonly isErr: true }\n```\n\nFunction Signature:\n\n```typescript\nconst  isErr: \u003cT, E\u003e(result: Result\u003cT, E\u003e): result is Err\u003cE\u003e\n```\n\nExample:\n\n```typescript\nimport { err } from 'resultage';\n\nconst result = err('Error message');\n\nif (result.isErr) {\n  console.error(result.error);\n} else {\n  console.log(result.value);\n}\n```\n\nExample with function:\n\n```typescript\nimport { err, isErr } from 'resultage';\n\nconst result = err('Error message');\n\nif (isErr(result)) {\n  console.error(result.error);\n} else {\n  console.log(result.value);\n}\n```\n\nThe function `isErr(result)` is good to be used as a callback in\nthe `Array.prototype.filter` method or similar.\n\n```typescript\nimport { isErr } from 'resultage';\n\nconst results = [ok(42), err('Error')];\n\nconst isSomethingWrong = results.some(isErr);\n```\n\n### Ok Property .value: T\n\nReturns the value of `Ok\u003cT\u003e`. Could be accessed if and only if the `Result\u003cT, S\u003e`\nis explicitly narrowed to `Ok\u003cT\u003e`.\n\nProperty Definition:\n\n```typescript\ninterface Ok\u003cT\u003e { readonly value: T }\n```\n\nExample:\n\n```typescript\nimport { ok } from 'resultage';\n\nconst result = ok(42);\n\nconsole.log(result.value); // Prints to console: 42\n```\n\nExample with narrowing:\n\n```typescript\nimport { ok, err } from 'resultage';\n\nconst okIfOdd = (value: number) =\u003e\n  value % 2 === 1\n    ? ok(value)\n    : err('Value is not odd');\n\nconst result = okIfOdd(43);\n\nresult.value;\n//     ^^^^^ - Error: Property 'value' does not exist on type 'Result\u003cnumber, string\u003e'.\n\nif (result.isOk) {\n  console.log(result.value);\n} else {\n  console.error(result.error);\n}\n```\n\n### Err Property .error: E\n\nReturns the error of `Err\u003cE\u003e`. Could be accessed if and only if the `Result\u003cT, S\u003e`\nis explicitly narrowed to `Err\u003cE\u003e`.\n\nProperty Definition:\n\n```typescript\ninterface Err\u003cE\u003e { readonly error: E }\n```\n\nExample:\n\n```typescript\nimport { err } from 'resultage';\n\nconst result = err('Error message');\n\nconsole.log(result.error); // Prints to console: Error message\n```\n\nExample with narrowing:\n\n```typescript\nimport { ok, err } from 'resultage';\n\nconst okIfOdd = (value: number) =\u003e\n  value % 2 === 1\n    ? ok(value)\n    : err('Value is not odd');\n\nconst result = okIfOdd(42);\n\nresult.error;\n//     ^^^^^ - Error: Property 'error' does not exist on type 'Result\u003cnumber, string\u003e'.\n\nif (result.isErr) {\n  console.error(result.error);\n} else {\n  console.log(result.value);\n}\n```\n\n### Method .map(fn)\n\nApplies `fn` to the value of `Ok\u003cT\u003e` and returns the value wrapped in `Ok\u003cS\u003e`. If `Result\u003cT, E\u003e` is `Err\u003cE\u003e` returns itself without applying `fn`.\n\nMethod Signature:\n\n```typescript\ninterface ResultInterface\u003cT, E\u003e {\n  map\u003cS\u003e(fn: (data: T) =\u003e S): Result\u003cS, E\u003e\n}\n```\n\nCurried Function Signature:\n\n```typescript\nconst map:\n  \u003cT, S\u003e(fn: (data: T) =\u003e S) =\u003e\n  \u003cE\u003e(result: Result\u003cT, E\u003e) =\u003e Result\u003cS, E\u003e\n```\n\nExample:\n\n```typescript\nimport { ok } from 'resultage';\n\nconst result = ok(42);\n\nconst mappedResult = result.map(value =\u003e value * 2);\n\nconsole.log(mappedResult.value); // Prints to console: 84\n```\n\n### Method .mapErr(fn)\n\nApplies `fn` to the value of `Err\u003cE\u003e` and returns the value wrapped in `Err\u003cF\u003e`. If `Result\u003cT, E\u003e` is `Ok\u003cT\u003e` returns itself without applying `fn`.\n\nMethod Signature:\n\n```typescript\ninterface ResultInterface\u003cT, E\u003e {\n  mapErr\u003cF\u003e(fn: (error: E) =\u003e F): Result\u003cT, F\u003e\n}\n```\n\nCurried Function Signature:\n\n```typescript\nconst mapErr:\n  \u003cE, F\u003e(fn: (error: E) =\u003e F) =\u003e\n  \u003cT\u003e(result: Result\u003cT, E\u003e) =\u003e Result\u003cT, F\u003e\n```\n\nExample:\n\n```typescript\nimport { err } from 'resultage';\n\nconst result = err('Error message');\n\nconst mappedResult = result.mapErr(error =\u003e new Error(error));\n```\n\n### Method .chain(next)\n\nApplies `next` to the value of `Ok\u003cT\u003e` and returns the result of `next`. If the `Result\u003cT, E\u003e` is `Err\u003cE\u003e`,\nreturns itself without applying `next`.\n\nThe next function must return a `Result\u003cS, F\u003e`.\n\nMethod Signature:\n\n```typescript\ninterface ResultInterface\u003cT, E\u003e {\n  chain\u003cS, F\u003e(next: (data: T) =\u003e Result\u003cS, F\u003e): Result\u003cS, E | F\u003e\n}\n```\n\nCurried Function Signature:\n\n```typescript\nconst chain:\n  \u003cT, S, F\u003e(next: (data: T) =\u003e Result\u003cS, F\u003e) =\u003e\n  \u003cE\u003e(result: Result\u003cT, E\u003e) =\u003e Result\u003cS, E | F\u003e\n```\n\nExample:\n\n```typescript\nimport { ok } from 'resultage';\n\nconst result = ok(42);\n\nconst chainedResult = result.chain(value =\u003e ok(value * 2));\n```\n\nThe `chain` method is a main method to compose `(...) =\u003e Result\u003cT, E\u003e` functions.\n\n### Method .chainErr(next)\n\nApplies `next` to the value of `Err\u003cE\u003e` and returns the result of `next`.\nIf the `Result\u003cT, E\u003e` is `Ok\u003cT\u003e`, returns itself without applying `next`.\n\nThe next function must return a `Result\u003cS, F\u003e`.\n\nMethod Signature:\n\n```typescript\ninterface ResultInterface\u003cT, E\u003e {\n  chainErr\u003cS, F\u003e(next: (error: E) =\u003e Result\u003cS, F\u003e): Result\u003cT | S, F\u003e\n}\n```\n\nCurried Function Signature:\n\n```typescript\nconst chainErr:\n  \u003cS, E, F\u003e(next: (error: E) =\u003e Result\u003cS, F\u003e) =\u003e\n  \u003cT\u003e(result: Result\u003cT, E\u003e) =\u003e Result\u003cT | S, F\u003e\n```\n\nExample:\n\n```typescript\nimport { err } from 'resultage';\n\nconst result = err('Error message');\n\nconst chainedResult = result.chainErr(error =\u003e err(new Error(error)));\n```\n\nThe `chainErr` is a convenient method to recover from an error.\n\n```typescript\nimport { err, ok } from 'resultage';\n\nconst okIfOdd = (value: number) =\u003e\n  value % 2 === 1\n    ? ok(value)\n    : err('Value is not odd');\n\nconst getOdd = (value: number): number =\u003e\n  okIfOdd(value)\n    .chainErr(() =\u003e ok(value + 1))\n    .unwrap();\n\nconsole.log(getOdd(1)); // 1\n```\n\n### Method .unwrap()\n\nReturns the value of `Ok\u003cT\u003e`. If the `Result\u003cT, E\u003e` is `Err\u003cE\u003e` throws a `TypeError`\nwhere `cause` is the result.\n\nMethod Signature:\n\n```typescript\ninterface ResultInterface\u003cT, E\u003e {\n  unwrap(): T\n}\n```\n\nFunction Signature:\n\n```typescript\nconst unwrap: \u003cT\u003e(result: Result\u003cT, unknown\u003e) =\u003e T\n```\n\nExample:\n\n```typescript\nimport { ok } from 'resultage';\n\nconst result = ok(42);\n\nconsole.log(result.unwrap()); // Prints to console: 42\n```\n\nExample with error:\n\n```typescript\nimport { err } from 'resultage';\n\nconst result = err('Error message');\n\nconsole.log(result.unwrap()); \n// Throws a TypeError with the message: 'Result is not an Ok' and cause equal\n// to the result.\n```\n\n### Method .unwrapOr(fallback)\n\nReturns the value of `Ok\u003cT\u003e`. If the `Result\u003cT, E\u003e` is `Err\u003cE\u003e` returns `fallback`.\n\nMethod Signature:\n\n```typescript\ninterface ResultInterface\u003cT, E\u003e {\n  unwrapOr\u003cS\u003e(fallback: S): T | S\n}\n```\n\nCurried Function Signature:\n\n```typescript\nconst unwrapOr:\n  \u003cT, S\u003e(fallback: S) =\u003e\n  (result: Result\u003cT, unknown\u003e) =\u003e T | S\n```\n\n### Method .unwrapOrThrow()\n\nReturns the value of `Ok\u003cT\u003e`. If the `Result\u003cT, E\u003e` is `Err\u003cE\u003e` throws a value of\ntype `E`.\n\n`unwrapOrThrow` doesn't check if `E` is an instance of `Error` or not, so it is\npossible to throw a non-error literal.\n\nMethod Signature:\n\n```typescript\ninterface ResultInterface\u003cT, E\u003e {\n  unwrapOrThrow(): T\n}\n```\n\nFunction Signature:\n\n```typescript\nconst unwrapOrThrow: \u003cT\u003e(result: Result\u003cT, unknown\u003e) =\u003e T\n```\n\n### Method .unwrapOrElse\n\nReturns the value of `Ok\u003cT\u003e`. If the `Result\u003cT, E\u003e` is `Err\u003cE\u003e` returns the result of `fallbackFn`.\n\nMethod Signature:\n\n```typescript\ninterface ResultInterface\u003cT, E\u003e {\n  unwrapOrElse\u003cS\u003e(fallbackFn: (error: E) =\u003e S): T | S\n}\n```\n\nCurried Function Signature:\n\n```typescript\nconst unwrapOrElse:\n  \u003cT, S\u003e(fallbackFn: (error: unknown) =\u003e S) =\u003e\n  (result: Result\u003cT, unknown\u003e) =\u003e T | S\n```\n\n### Method .unwrapErr\n\nReturns the value of `Err\u003cE\u003e`. If the `Result\u003cT, E\u003e` is `Ok\u003cT\u003e` throws a `TypeError` where `cause` is the `Ok\u003cT\u003e`.\n\nMethod Signature:\n\n```typescript\ninterface ResultInterface\u003cT, E\u003e {\n  unwrapErr(): E\n}\n```\n\nFunction Signature:\n\n```typescript\nconst unwrapErr: \u003cE\u003e(result: Result\u003cunknown, E\u003e) =\u003e E\n```\n\n### Method .unwrapErrOr(fallback)\n\nReturns the value of `Err\u003cE\u003e`. If the `Result\u003cT, E\u003e` is `Ok\u003cT\u003e` returns `fallback`.\n\nMethod Signature:\n\n```typescript\ninterface ResultInterface\u003cT, E\u003e {\n  unwrapErrOr\u003cF\u003e(fallback: F): E | F\n}\n```\n\nCurried Function Signature:\n\n```typescript\nconst unwrapErrOr:\n  \u003cF\u003e(fallback: F) =\u003e\n  \u003cT, E\u003e(result: Result\u003cT, E\u003e) =\u003e E | F\n```\n\n### Method .unwrapErrOrElse(fallbackFn)\n\nReturns the value of `Err\u003cE\u003e`. If the `Result\u003cT, E\u003e` is `Ok\u003cT\u003e` returns the result of `fallback`.\n\nMethod Signature:\n\n```typescript\ninterface ResultInterface\u003cT, E\u003e {\n  unwrapErrOrElse\u003cF\u003e(fallbackFn: (data: T) =\u003e F): E | F\n}\n```\n\nCurried Function Signature:\n\n```typescript\nconst unwrapErrOrElse:\n  \u003cF, T\u003e(fallbackFn: (data: T) =\u003e F) =\u003e\n  \u003cE\u003e(result: Result\u003cT, E\u003e) =\u003e E | F\n```\n\n### Method .unpack()\n\nReturns the value of `Ok\u003cT\u003e` or `Err\u003cE\u003e`.\n\nMethod Signature:\n\n```typescript\ninterface ResultInterface\u003cT, E\u003e {\n  unpack(): T | E\n}\n```\n\nFunction Signature:\n\n```typescript\nconst unpack: \u003cT, E\u003e(result: Result\u003cT, E\u003e) =\u003e T | E\n```\n\n### Method .match(okMatcher, errMatcher)\n\nApplies `okMatcher` to the value of `Ok\u003cT\u003e` and returns the result. Applies `errMatcher` to the value of `Err\u003cE\u003e` and returns the result.\n\nMethod Signature:\n\n```typescript\ninterface ResultInterface\u003cT, E\u003e {\n  match\u003cS, F\u003e(okMatcher: (data: T) =\u003e S, errMatcher: (error: E) =\u003e F): S | F\n}\n```\n\nCurried Function Signature:\n\n```typescript\nconst match:\n  \u003cT, S, E, F\u003e(okMatcher: (data: T) =\u003e S, errMatcher: (error: E) =\u003e F) =\u003e\n  (result: Result\u003cT, E\u003e) =\u003e S | F\n```\n\n### Method .tap(fn)\n\nApplies `fn` to the value of `Ok\u003cT\u003e` and returns the original result. If the `Result\u003cT, E\u003e` is `Err\u003cE\u003e` doesn't apply `fn`.\n\nMethod Signature:\n\n```typescript\ninterface ResultInterface\u003cT, E\u003e {\n  tap(fn: (data: T) =\u003e void): Result\u003cT, E\u003e\n}\n```\n\nCurried Function Signature:\n\n```typescript\nconst tap:\n  \u003cT\u003e(fn: (data: T) =\u003e void) =\u003e\n  \u003cE\u003e(result: Result\u003cT, E\u003e) =\u003e Result\u003cT, E\u003e\n```\n\n### Method .tapErr(fn)\n\nApplies `fn` to the value of `Err\u003cE\u003e` and returns the original result. If the `Result\u003cT, E\u003e` is `Ok\u003cT\u003e` doesn't apply `fn`.\n\nMethod Signature:\n\n```typescript\ninterface ResultInterface\u003cT, E\u003e {\n  tapErr(fn: (error: E) =\u003e void): Result\u003cT, E\u003e\n}\n```\n\nCurried Function Signature:\n\n```typescript\nconst tapErr:\n  \u003cE\u003e(fn: (error: E) =\u003e void) =\u003e\n  \u003cT\u003e(result: Result\u003cT, E\u003e) =\u003e Result\u003cT, E\u003e\n```\n\n## Operating on Multiple Results\n\n### collect(results)\n\nCollects `Ok\u003cT\u003e` values from an array of `Result\u003cT, E\u003e` and returns a `Result\u003cT[], E\u003e`.\n\nFunction Signature:\n\n```typescript\nconst collect:\n  \u003cR extends readonly Result\u003cany, any\u003e[]\u003e(results: R) =\u003e Result\u003cCollected\u003cR\u003e, ErrTypeOf\u003cR[number]\u003e\u003e\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdscheglov%2Fresultage","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdscheglov%2Fresultage","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdscheglov%2Fresultage/lists"}