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

goTryRaw types are not developer friendly #3

Closed
IlyaSemenov opened this issue Jul 4, 2024 · 2 comments
Closed

goTryRaw types are not developer friendly #3

IlyaSemenov opened this issue Jul 4, 2024 · 2 comments

Comments

@IlyaSemenov
Copy link

Consider this:

async function fn(a: number) {
  if (a < 0) {
    throw new MyError()
  }
  return otherFn(a) // known to return Promise<ComplexType>
}

const [err, data] = await goTryRaw(fn(123))
  • data is typed as ComplexType, which is good.
  • err is typed as unknown, which is inconvenient.

Now as I know that fn throws MyError, I want to type the return value:

const [err, data] = await goTryRaw<MyError>(fn(123))
  • err is typed as MyError
  • data is typed as unknown, which is inconvenient.

Sometypes ComplexType is not even exported as it may be a complex generic created inside a third party library, so it couldn't even be retrieved with ReturnType<...> escape hatch.

So basically developer is supposed to use goTryRaw without arguments and then cast error to proper type.

Ideally, there should be a way to provide error type but automatically infer return value type. I am not sure that elegant solution to this even exists, but wanted to report this concern nevertheless.

@thelinuxlich
Copy link
Owner

Hey @IlyaSemenov I agree with you! But it's not possible until TS adds this feature: microsoft/TypeScript#26242

@thelinuxlich
Copy link
Owner

What you can do is pass a second generic parameter with ComplexType:

const [err, data] = await goTryRaw<MyError, ComplexType>(fn(123))

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

No branches or pull requests

2 participants