Skip to content

proposal: @std/result, a plain-object Result<T, E> with tryCatch #7305

Open

Description

@tomas-zijdemans

Is your feature request related to a problem? Please describe.

Everyone hand-rolls the same Result type. JSR has 30+ @*/result packages. Theo Browne's tryCatch gist has 877 stars and 133 forks, each fork with a slightly different shape.

Describe the solution you'd like

A small, unstable @std/result. Plain objects, no classes, structuredClone and JSON safe:

type Result<T, E = Error> = { ok: true; value: T } | { ok: false; error: E };

Field names follow the TC39 try operator (ok / value / error), so this is the future shape, not a competing one. On top: ok, err, isOk, isErr, tryCatch, map, mapErr, andThen, unwrapOr, match. That's the whole package.

No generators, no do-notation, no fluent chains. Those belong in neverthrow and Effect.

#525 turned down the Go tuple in 2019 as non-idiomatic. Agreed. A discriminated union narrows, a tuple doesn't.

Happy to open a PR if there's appetite.

Describe alternatives you've considered

  • neverthrow, Effect, better-result: class-based, bigger, and std shouldn't bless one vendor.
  • Wait for the try operator: Stage 0, no champion.
  • Keep hand-rolling: the status quo, and the reason for this issue.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions