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

Improve function return types #61

Closed
wants to merge 1 commit into from
Closed

Improve function return types #61

wants to merge 1 commit into from

Conversation

mircea-pavel-anton
Copy link
Contributor

What kind of change does this PR introduce?

What is the current behavior?

Fixes #55

Currently, the return type for most functions is:

Promise<{ data: { Key: string } | null; error: Error | null }>

Which means that a return type of either {null, null} or {{key: string}, Error} is technically possible even though it would not make any sense, so we have to manually check that data AND error are not null.

What is the new behavior?

By changing the return types to something like:

Promise<{ data: { Key: string }; error: null } | { data: null; error: Error }>

We basically narrow it down to data or error, not data and/or error as a possible return.
This also makes sure that we don't accidentally return {null, null}, for example, which would not have been reported as an error by typescript before.

Additional context

I also noticed that most of the try... catch blocks would show a warning because the error variable in catch (error) is not typed so it was reporting as unknown and unknown cannot be cast to either Error or null. As such, I also modified all try... catch statements to cast error as Error in the meantime. Eventually, some custom errors would be required, i think:

try {
...
} catch (error: any) {
  return { data: null, error } // no longer showing a warning for error being an unknown type
}

@alaister
Copy link
Member

alaister commented Apr 8, 2022

Hey @mirceanton,

Thanks for the PR! Unfortunately, it looks like we're working on the same problem simultaneously as there is already a PR open for this: #60.

I'm closing this in favour of #60, but I'd be very appreciative of any feedback you give over there!

@alaister alaister closed this Apr 8, 2022
@mircea-pavel-anton
Copy link
Contributor Author

Oh, right. I didn't notice because the issue was not linked to the PR

@mircea-pavel-anton mircea-pavel-anton deleted the feat/return_types branch April 8, 2022 13:27
@alaister
Copy link
Member

alaister commented Apr 8, 2022

Sorry about that - I've updated it.

@mircea-pavel-anton
Copy link
Contributor Author

To be fair, the PR is pretty self descriptive in it's title :)) I guess I didn't really check already existing PRs as I noticed no linked ones

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.

Use enum to narrow down return type
2 participants