Skip to content

chore: Bump typescript version to support type safe destructure of return value #31

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,19 @@ console.log(data)
```

```ts
const result = await until(() => action())
const { data, error } = await until(() => action())

// At this point, "data" is ambiguous "DataType | null"
// which is correct, as you haven't checked nor handled the "error".
// which is correct, as you haven't checked or handled the "error".

if (result.error) {
if (error) {
return null
}

// Data is strict "DataType" since you've handled the "error" above.
console.log(result.data)
console.log(data)
```

> It's crucial to keep the entire result of the `Promise` in a single variable and not destructure it. TypeScript will always keep the type of `error` and `data` as it was upon destructuring, ignoring any type guards you may perform later on.

## Special thanks

- [giuseppegurgone](https://twitter.com/giuseppegurgone) for the discussion about the original `until` API.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
"jest": "^26.6.3",
"ts-jest": "^26.5.4",
"tsup": "^6.2.3",
"typescript": "^4.2.4"
"typescript": "^4.6.4"
},
"scripts": {
"start": "tsc -w",
"test": "jest",
"build": "tsup",
"release": "release publish"
}
}
}
9 changes: 6 additions & 3 deletions src/until.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ export type AsyncTuple<

/**
* Gracefully handles a given Promise factory.
* @template ErrorType The type of the error promise may throw.
* @template DataType The type of the data promise may return.
* @param promise function that returns a promise.
* @example
* const { error, data } = await until(() => asyncAction())
* const { error, data } = await until<UserFetchError, User>(() => fetchUser(id))
*/
export const until = async <
ErrorType extends any = Error,
Expand All @@ -20,11 +23,11 @@ export const until = async <
promise: () => Promise<DataType>,
): Promise<AsyncTuple<ErrorType, DataType>> => {
try {
const data = await promise().catch((error) => {
const data = await promise().catch((error: ErrorType) => {
throw error
})
return { error: null, data }
} catch (error) {
return { error, data: null }
return { error: error as ErrorType, data: null }
}
}