{"id":23767776,"url":"https://github.com/misuken-now/smart-result","last_synced_at":"2025-09-05T12:31:10.620Z","repository":{"id":230020172,"uuid":"778085722","full_name":"misuken-now/smart-result","owner":"misuken-now","description":"very smart result type","archived":false,"fork":false,"pushed_at":"2024-03-27T11:25:37.000Z","size":24,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-08-09T02:58:07.337Z","etag":null,"topics":["error-handling","result","result-type","results","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/misuken-now.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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}},"created_at":"2024-03-27T03:34:24.000Z","updated_at":"2024-08-09T02:58:07.338Z","dependencies_parsed_at":"2024-03-27T12:50:11.664Z","dependency_job_id":null,"html_url":"https://github.com/misuken-now/smart-result","commit_stats":null,"previous_names":["misuken-now/smart-result"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/misuken-now%2Fsmart-result","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/misuken-now%2Fsmart-result/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/misuken-now%2Fsmart-result/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/misuken-now%2Fsmart-result/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/misuken-now","download_url":"https://codeload.github.com/misuken-now/smart-result/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":232039703,"owners_count":18464029,"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":["error-handling","result","result-type","results","typescript"],"created_at":"2025-01-01T00:48:08.338Z","updated_at":"2025-01-01T00:48:08.844Z","avatar_url":"https://github.com/misuken-now.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# smart-result\n\nThis type library is a very smart result type available in TypeScript 4.6 or later.\n\n```\nimport type { Result } from \"smart-result\";\n\nasync function loadData(): Promise\u003cResult\u003cData, Error\u003e\u003e {\n    try {\n        const response = await client.get();\n        return { ok: response.data };\n    } catch (error) {\n        if (error instanceof Error) {\n            return { error: error };\n        }\n        return { error: new Error(\"unknown error\") };\n    }\n}\n\n\nasync function example() {\n    const { ok: data, error } = await loadData();\n    if (data) {\n        console.log(data); // data: Data; error: undefined;\n    } else {\n        console.error(error); // data: undefined; error: Error;\n    }\n}\n```\n\n## Highlight\n\n- 🖊 Good coding quality\n- 👀 Highly readable\n- 😄 Fewer things to remember\n- ✅ Safety design\n- 🛡 Type only\n\n## Intall\n\n```\nnpm install -D `smart-result`\n```\n\nor\n\n```\nyarn add -D `smart-result`\n```\n\n## Usage\n\n### Void Pattern\n\n```\nimport type { Result } from \"smart-result\";\n\nasync function start(): Promise\u003cResult\u003ctrue, Error\u003e\u003e {\n    try {\n        await client.start();\n        return { ok: true };\n    } catch (error) {\n        if (error instanceof Error) {\n            return { error: error };\n        }\n        return { error: new Error(\"unknown error\") };\n    }\n}\n\nasync function example() {\n    // Use \"ok\" if you only want to know if you succeeded or not\n    const { ok, error } = await start();\n    if (ok) {\n        console.log(ok); // ok: true;\n    } else {\n        console.error(error); // ok: undefined; error: Error;\n    }\n}\n```\n\n### Return Data Pattern\n\n```\nimport type { Result } from \"smart-result\";\n\nasync function loadData(): Promise\u003cResult\u003cData, Error\u003e\u003e {\n    try {\n        const response = await client.get();\n        return { ok: response.data };\n    } catch (error) {\n        if (error instanceof Error) {\n            return { error: error };\n        }\n        return { error: new Error(\"unknown error\") };\n    }\n}\n\nasync function example() {\n    // Assign a name to \"ok\" if you want to handle the resulting information\n    const { ok: data, error } = await loadData();\n    if (data) {\n        console.log(data); // data: Data; error: undefined;\n    } else {\n        console.error(error); // data: undefined; error: Error;\n    }\n}\n```\n\n### Return Value Pattern\n\n```\nimport type { Result } from \"smart-result\";\n\nasync function loadValue(): Promise\u003cResult\u003c{ value: number }, Error\u003e\u003e {\n    try {\n        const response = await client.get();\n        // If you want to return a primitive value, wrap it with `{ value: }`.\n        return { ok: { value: response.data } };\n    } catch (error) {\n        if (error instanceof Error) {\n            return { error: error };\n        }\n        return { error: new Error(\"unknown error\") };\n    }\n}\n\nasync function example() {\n    const { ok, error } = await loadValue();\n    if (ok) {\n        console.log(ok.value); // ok: { value: number } error: undefined;\n    } else {\n        console.error(error); // data: undefined; error: Error;\n    }\n}\n```\n\n## API\n\n### `Result\u003cData extends true | Record\u003cstring, any\u003e, Error\u003e`\n\nAlias for `OkResult\u003cData\u003e | ErrorResult\u003cError\u003e`.\n\n### `OkResult\u003cData extends true | Record\u003cstring, any\u003e\u003e`\n\nAn object with the key `ok` and without the key `error`.\n\n### `ErrorResult\u003cError extends Record\u003cstring, any\u003e\u003e\u003e`\n\nAn object with the key `error` and without the key `ok`.\n\n## LICENSE\n\n[@misuken-now/smart-result](https://github.com/misuken-now/smart-result)・MIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmisuken-now%2Fsmart-result","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmisuken-now%2Fsmart-result","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmisuken-now%2Fsmart-result/lists"}