Repository navigation
Proposal: Add an error mapper overload to attemptAsync #2051
minseong0324
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
attemptAsynccan infer the success type from the return type of the callback:However, the error type cannot be inferred from the callback because
Promise<T>does not include a rejection type and TypeScript does not infer the types of thrown valuesThe error type can be supplied explicitly:
But this only tells TypeScript that the error is a
RequestError. It does not transform or validate the value caught at runtimeWould it make sense to add an overload that accepts an error mapper?
For example:
Twould be inferred fromrequest, whileEwould be inferred from the return type ofmapError. The mapper would also ensure that the runtime value matches the inferred error typeCalls without the second argument would keep the existing behavior
I put together a rough PoC here: https://github.com/minseong0324/es-toolkit/tree/attempt-async-error-mapper
Would this overload be a reasonable fit for
attemptAsync?All reactions