{"_id":"@alexanderhott/resultts","name":"@alexanderhott/resultts","dist-tags":{"latest":"0.1.0"},"versions":{"0.1.0":{"name":"@alexanderhott/resultts","version":"0.1.0","description":"A simple, easy to use, error handling library for typescript.","homepage":"https://github.com/AlexanderHOtt/resultts#readme","bugs":{"url":"https://github.com/AlexanderHOtt/resultts"},"license":"MIT","author":{"name":"Alexander Ott","email":"66271487+AlexanderHott@users.noreply.github.com"},"repository":{"type":"git","url":"git+https://github.com/AlexanderHOtt/resultts.git"},"type":"module","exports":{".":"./dist/index.mjs","./package.json":"./package.json"},"publishConfig":{"access":"public"},"scripts":{"build":"vp pack","dev":"vp pack --watch","test":"vp test","check":"vp check","prepublishOnly":"vp run check && vp run build"},"devDependencies":{"@types/node":"^25.5.0","@typescript/native-preview":"7.0.0-dev.20260328.1","bumpp":"^11.0.1","typescript":"^6.0.2","vite-plus":"^0.1.14"},"packageManager":"pnpm@10.33.2","pnpm":{"overrides":{"vite":"npm:@voidzero-dev/vite-plus-core@latest","vitest":"npm:@voidzero-dev/vite-plus-test@latest"}},"gitHead":"64b2965df66e3a09325276ca95b322c819615878","_id":"@alexanderhott/resultts@0.1.0","_nodeVersion":"24.16.0","_npmVersion":"11.13.0","dist":{"integrity":"sha512-Jzx5rKRgYPfTxHZrOv+dLCx4pdxujnRNTQWBfr4P6Nq8yXfajO4C8g9+QNPHH48Fi4fCsgFUZvGZ6YzIS9n4TQ==","shasum":"212987b3e4fa655cb3f9a3cb9b4108d323873857","tarball":"https://registry.npmjs.org/@alexanderhott/resultts/-/resultts-0.1.0.tgz","fileCount":5,"unpackedSize":19059,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQCpJIGoRpDtiLdhp6gf1NXUgPb4+4mppEMjQYaS83pNbgIgFPUnRuOQx3dLLUFAqiFnHLYsWAwDyNaxa5Rfyo6MRGw="}]},"_npmUser":{"name":"alexanderhott","email":"aott01@protonmail.ch"},"directories":{},"maintainers":[{"name":"alexanderhott","email":"aott01@protonmail.ch"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/resultts_0.1.0_1779494468109_0.6933360329045959"},"_hasShrinkwrap":false}},"time":{"created":"2026-05-23T00:01:07.912Z","0.1.0":"2026-05-23T00:01:08.301Z","modified":"2026-05-23T00:01:08.583Z"},"maintainers":[{"name":"alexanderhott","email":"aott01@protonmail.ch"}],"description":"A simple, easy to use, error handling library for typescript.","homepage":"https://github.com/AlexanderHOtt/resultts#readme","repository":{"type":"git","url":"git+https://github.com/AlexanderHOtt/resultts.git"},"author":{"name":"Alexander Ott","email":"66271487+AlexanderHott@users.noreply.github.com"},"bugs":{"url":"https://github.com/AlexanderHOtt/resultts"},"license":"MIT","readme":"<div align=\"center\"><h1>resultts</h1></div>\n\nA simple, easy to use, error handling library for typescript.\n\n## API\n\n```ts\nimport {\n  AnyError,\n  type AnyResult,\n  Err,\n  type Result,\n  Ok,\n  err,\n  errAny,\n  ok,\n  tryAsync,\n  trySync,\n} from \"resultts\";\n```\n\n### `Result<T, E>`\n\n`Result<T, E>` is `Ok<T> | Err<E>`. Return `Ok` for success and `Err` for\nexpected, recoverable failure.\n\n```ts\ntype ParsePortError =\n  | { kind: \"NOT_AN_INTEGER\"; input: string }\n  | { kind: \"OUT_OF_RANGE\"; port: number };\n\nfunction parsePort(input: string): Result<number, ParsePortError> {\n  const port = Number(input);\n\n  if (!Number.isInteger(port)) {\n    return err({ kind: \"NOT_AN_INTEGER\", input });\n  }\n\n  if (port < 1 || port > 65535) {\n    return err({ kind: \"OUT_OF_RANGE\", port });\n  }\n\n  return ok(port);\n}\n```\n\n### `ok(value)` and `err(error)`\n\nUse `ok` and `err` to create results. Use `isOk()` and `isErr()` to narrow the\ntype before reading `.value` or `.error`.\n\n```ts\nconst result = parsePort(\"3000\");\n\nif (result.isOk()) {\n  console.log(result.value);\n} else {\n  console.log(result.error.kind);\n}\n```\n\n### `trySync(fn, mapErr?)`\n\nUse `trySync` to convert throwing synchronous code into a `Result`.\n\n```ts\nconst configResult = trySync(\n  () => JSON.parse(text),\n  (cause) => ({ kind: \"INVALID_JSON\", cause }) as const,\n);\n```\n\n### `tryAsync(fn, mapErr?)`\n\nUse `tryAsync` to convert rejecting async code into a\n`Promise<Result<T, E>>`.\n\n```ts\nconst responseResult = await tryAsync(\n  () => fetch(\"https://example.com/config.json\"),\n  (cause) => ({ kind: \"FETCH_FAILED\", cause }) as const,\n);\n```\n\nPass a function to `tryAsync`, not an already-started promise, so synchronous\nthrows and promise rejections are caught in the same place.\n\n### `AnyError`, `AnyResult<T>`, and `errAny(message)`\n\nUse `AnyError` when a function does not need a typed error union and only wants\nto add human-readable context while returning failures upward.\n\n```ts\nfunction readRequiredEnv(name: string): AnyResult<string> {\n  const value = process.env[name];\n  if (value === undefined) {\n    return errAny(`missing environment variable: ${name}`);\n  }\n\n  return ok(value);\n}\n```\n\n`Err.context(message)` converts any error value into an `AnyError` and stores\nthe previous error as the cause.\n\n```ts\nconst portResult = readRequiredEnv(\"PORT\").context(\"reading server config\");\n```\n\n## Error shape recommendations\n\nPrefer plain object error variants for expected failures:\n\n```ts\ntype CreateUserError =\n  | { kind: \"EMAIL_TAKEN\"; email: string }\n  | { kind: \"INVALID_EMAIL\"; email: string }\n  | { kind: \"DATABASE_WRITE_FAILED\"; cause: unknown };\n```\n\nAvoid modeling expected `Result` errors as subclasses of `Error`.\n\n`Error` subclasses are useful at process boundaries and for unexpected\nexceptions, but they are a poor default for typed, recoverable domain failures:\n\n- `instanceof` can become unreliable across package copies, realms, test\n  sandboxes, workers, and mixed ESM/CJS boundaries.\n- subclass hierarchies make exhaustiveness checking weaker than discriminated\n  unions with a `kind` field.\n- structured data tends to get hidden behind `message`, making programmatic\n  handling and tests more awkward.\n- stack traces add noise for errors that are ordinary control-flow outcomes,\n  such as validation failures or missing records.\n\nUse an `Error` value as a `cause` when you caught an exception from another API.\nUse `AnyError` when you intentionally want a general, contextual error chain.\nFor domain errors that callers are expected to inspect, prefer a discriminated\nunion of plain objects.\n\nSee `examples/` for more.\n\n## Why not `ResultAsync`?\n\nSome Result libraries use a `ResultAsync<T, E>` wrapper instead of\n`Promise<Result<T, E>>`. `ResultAsync` can make chained code feel nicer:\n\n```ts\nreturn readFileSafe(path).context(\"reading config file\").andThen(parseConfig);\n```\n\nThe tradeoff is that `ResultAsync` does not fit as naturally with JavaScript's\nnative `async` functions. An `async function` always returns a `Promise`, so a\nfunction that returns `ResultAsync<T, E>` usually cannot use `await` directly\ninside its body:\n\n```ts\nasync function readConfig(): ResultAsync<Config, Error> {\n  // This shape does not work: async functions return Promise<...>.\n}\n```\n\nThat tends to push code toward callback-style chains:\n\n```ts\nfunction readConfig(): ResultAsync<Config, Error> {\n  return readFileSafe(path).andThen(parseConfig);\n}\n```\n\nThis can be a good fit for linear pipelines, but it is less comfortable for\nordinary JavaScript control flow with local variables, loops, branching, and\nmultiple `await` points.\n\nUsing `Promise<Result<T, E>>` keeps the library closer to the platform:\n\n```ts\nasync function readConfig(): Promise<Result<Config, Error>> {\n  const textResult = await readFileSafe(path);\n  if (textResult.isErr()) {\n    return textResult;\n  }\n\n  return parseConfig(textResult.value);\n}\n```\n\nThe downside is more explicit early-return code. The upside is simpler interop\nwith existing Promise APIs and normal `async` / `await` functions.\n\n## Development\n\nInstall [`vp`](https://viteplus.dev/guide/), the vite+ cli.\n\n- Install dependencies:\n\n```bash\nvp install\n```\n\n- Run the unit tests:\n\n```bash\nvp test\n```\n\n- Build the library:\n\n```bash\nvp pack\n```\n","readmeFilename":"README.md","_rev":"1-3db5c390cf476572223b9ce48748fe02"}