{"id":27603800,"url":"https://github.com/constraintautomaton/result-interface-ts","last_synced_at":"2025-04-22T19:19:36.611Z","repository":{"id":286431497,"uuid":"961384564","full_name":"constraintAutomaton/result-interface-ts","owner":"constraintAutomaton","description":"A tiny utility to standardize how to handle results that may succeed or fail inspired by Go-style error handling.","archived":false,"fork":false,"pushed_at":"2025-04-17T18:16:21.000Z","size":45,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-22T19:19:31.908Z","etag":null,"topics":["type-safety","typescript","typing"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/result-interface","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/constraintAutomaton.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,"zenodo":null}},"created_at":"2025-04-06T12:07:37.000Z","updated_at":"2025-04-17T18:16:25.000Z","dependencies_parsed_at":null,"dependency_job_id":"6c6e2f56-afe5-477d-8339-580d0f26dcab","html_url":"https://github.com/constraintAutomaton/result-interface-ts","commit_stats":null,"previous_names":["constraintautomaton/result-interface-ts"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/constraintAutomaton%2Fresult-interface-ts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/constraintAutomaton%2Fresult-interface-ts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/constraintAutomaton%2Fresult-interface-ts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/constraintAutomaton%2Fresult-interface-ts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/constraintAutomaton","download_url":"https://codeload.github.com/constraintAutomaton/result-interface-ts/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250306602,"owners_count":21408927,"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":["type-safety","typescript","typing"],"created_at":"2025-04-22T19:19:35.963Z","updated_at":"2025-04-22T19:19:36.600Z","avatar_url":"https://github.com/constraintAutomaton.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# result-interface\n\n![npm version](https://img.shields.io/npm/v/result-interface)\n![Unit Tests Status](https://img.shields.io/github/actions/workflow/status/constraintAutomaton/result-interface-ts/ci.yml?label=unit+test\n)\n\nA tiny utility (mainly interfaces) with zero dependencies to standardize handling results that may succeed or fail, inspired by Go-style error handling.\n\n## Installation\n\n```bash\nnpm i result-interface\n```\n\n## Usage\n\nYou can define functions more declaratively when working with possible failures:\n\n```ts\nimport { type Result, isError } from \"result-interface\";\n\nlet VALUE: number | undefined = undefined;\n\nfunction getValue(): Result\u003cnumber, string\u003e {\n    if (VALUE !== undefined) {\n        return { value: VALUE };\n    }\n    return { error: \"The value is undefined\" };\n}\n\nconst resp: Result\u003cnumber, string\u003e = getValue();\n\nif (isError(resp)) {\n    console.log(`Unable to get the value. Reason: ${resp.error}`);\n    process.exit(1);\n}\n\nconsole.log(`The value multiplied by two is ${resp.value * 2}`);\n```\nYou can specify that a `Promise will not throw`:\n\n```ts\nimport { isError, SafePromise, type Result } from \"result-interface\";\n\nlet VALUE: number | undefined = undefined;\n\nasync function getValueLater(): SafePromise\u003cnumber,string\u003e {\n    return new Promise((resolve, reject) =\u003e {\n        if (VALUE !== undefined) {\n            resolve({\n                value:VALUE\n            })\n        } else {\n            reject(\"The value is undefined\");\n        }\n    });\n}\n\n// Creates a promise that always resolves with a Result. \n// On failure, it resolves with an error instead of rejecting or throwing.\nconst resp: Result\u003cnumber, string\u003e = await getValueLater();\n\nif (isError(resp)) {\n    console.log(`Unable to get the value. Reason: ${resp.error}`);\n    process.exit(1);\n}\n\nconsole.log(`The value multiplied by two is ${resp.value * 2}`);\n```\n\nYou can specify that a promise will never fail, thus that it will always be an `IResult`:\n```ts\nimport { SafePromise, type Result } from \"./src/index\";\n\nlet VALUE: number | undefined = undefined;\n\nasync function getValueLater(): SafePromise\u003cnumber, never\u003e {\n    return new Promise((resolve, reject) =\u003e {\n        if (VALUE !== undefined) {\n            resolve({\n                value: VALUE\n            })\n        } else {\n            reject(\"The value is undefined\");\n        }\n    });\n}\n\n// Creates a promise that always resolves with a Result. \n// On failure, it resolves with an error instead of rejecting or throwing.\nconst resp: Result\u003cnumber, never\u003e = await getValueLater();\n\nconsole.log(`The value multiplied by two is ${resp.value * 2}`);\n```\nYou can ensure that a `Promise` will not throw:\n\n```ts\nimport { isError, safePromise, type Result } from \"result-interface\";\n\nlet VALUE: number | undefined = undefined;\n\nasync function getValueLaterUnsafe(): Promise\u003cnumber\u003e {\n    return new Promise((resolve, reject) =\u003e {\n        if (VALUE !== undefined) {\n            resolve(VALUE)\n        } else {\n            reject(\"The value is undefined\");\n        }\n    });\n}\n\n// Creates a promise that always resolves with a Result. \n// On failure, it resolves with an error instead of rejecting or throwing.\nconst resp: Result\u003cnumber, unknown\u003e = await safePromise(getValueLaterUnsafe());\n\nif (isError(resp)) {\n    console.log(`Unable to get the value. Reason: ${resp.error}`);\n    process.exit(1);\n}\n\nconsole.log(`The value multiplied by two is ${resp.value * 2}`);\n```\n\nYou can use helper functions to generate `IError` and `IResult` types (the possible types of `Result`).\n\n```ts\nimport { type Result, isError, result, error } from \"result-interface\";\n\nfunction getValue(): Result\u003cnumber, string\u003e {\n    if (VALUE !== undefined) {\n        return result(Value);\n    }\n    return error(\"The value is undefined\");\n}\n```\n\n## Test\n\n```bash\nbun test\n```\n## License\n\nThis project is licensed under the [MIT License](./LICENSE). See the LICENSE file for more details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fconstraintautomaton%2Fresult-interface-ts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fconstraintautomaton%2Fresult-interface-ts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fconstraintautomaton%2Fresult-interface-ts/lists"}