Pass typing-helpers to safeTry's body
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.
The example you have given does pass type checks for me. Maybe it depends on the version of TypeScript that's being used?
EDIT: I just checked, all of the examples given do pass type checks: https://www.typescriptlang.org/play?ts=5.2.2#code/JYWwDg9gTgLgBAbzhA1gGjgUylDBnAQwDNMAVKATzgF84ioIQ4ByAO0wDdsYALBgd2YBuAFAjCJchQA8SGBTCYAXHABERCBFU0MAV1YpWEfqwB8ACiL6AxjGARWAKjjmAlIhFwvcKJhi6oVmQUczkFZTUNLRpXEWpY8WIySml9Q2NWDDDFFXVNbWoLK1ZbeycXdwRPbwB6GrhSHmA8OAADbChWuGa4fh4CeB7MAA9IWEwAEzgAIyp2Llg+Y2qvX39ArBxQuHkcyPyYuISJZJk0oxMsnfDcqIKimzsHZzcPbzgKYEwAGwnnDvM+gmmCIwHYE1cADoQAQwABRLavAC8phcVXe712ETyWhWMShJwAqqx+FBYW48WsAkFUOZVHhGH4mqwAOaqWLxERAA
@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.
@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.