Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass typing-helpers to safeTry's body #500

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

tsuburin
Copy link
Contributor

@tsuburin tsuburin commented Oct 26, 2023

Edit: The descriptions for ok and err in this comment is wrong. Please see the following comments.

With safeTry, sometimes we need to specify type parameters to pass type-checks.
For example, the following will not pass type-checks because the body's return type (T) is inferred to be { type: string }

safeTry<{ type: "foo" }, unknown>(function*() {
    return ok({ type: "foo" })
})

A naive solution is to specify the body's type parameters like

safeTry<{ type: "foo" }, unknown>(function*(): Generator<Err<never, unknown>, Result<{ type: "foo" }, unknown>> {
    return ok({ type: "foo" })
})

but this requires many keystrokes and obviously cumbersome.

With this PR, properly typed helpers will be passed to the body so that we will not be required to specify type parameters.
For example,

safeTry<{ type: "foo" }, unknown>(function*({
  ok  // This `ok` is `ok<{ type: "foo" }, unknown>`, so using this we don't need to specify type parameters anymore
}) {
    return ok({ type: "foo" })
})

The helpers are ok, err and error. Please see the comments on the code.

This is not a breaking change.

src/result.ts Outdated Show resolved Hide resolved
src/result.ts Outdated Show resolved Hide resolved
@tsuburin
Copy link
Contributor Author

tsuburin commented Oct 27, 2023

@ghost91-

The example you have given does pass type checks for me. Maybe it depends on the version of TypeScript that's being used?

Thank you for taking your time to try reproducing it. I'm not sure I understand what happens here because I'm not used to using playground with imports, but it seems not working in those cases because it seems this also passes the check. (I say 'seems' because I could not find any compilation outputs, and the only clue I had was that no red underlines showed up there.)

However, I also confirmed that my examples do pass with the version of Typescript of this repository by adding them in tests/safe-try.test.ts. Maybe it depends on version of Typescript, as you said, or perhaps I did too much while I'm simplifying examples to write this PR, but anyway I should have checked more closely, I'm sorry. One example confirmed to not pass is in #501.

I'll fix examples and documents in this PR. Thank you.

@tsuburin
Copy link
Contributor Author

tsuburin commented Oct 29, 2023

@ghost91-
I fixed documents and examples.
For error, I confirmed the fixed example does not pass type-checks with the version of Typescript used in this repository.
For ok and err, I could not find good examples with simple types I tried, so it seems to be my misunderstanding. Or maybe I could find by using complex types for T or E, but aside from that, I still have another good reason for those helpers, so I modified the documents. That is, if a wrong value is returned from the body, we get puzzling error messages emitted not at the place where the wrong value is returned but at the body itself. This makes engineering experience really bad, and these helpers improve it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants