Skip to content

Instantly share code, notes, and snippets.

@Dionid
Created October 22, 2021 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dionid/d67aa910c47f255bd70f3f9c8836a913 to your computer and use it in GitHub Desktop.
Save Dionid/d67aa910c47f255bd70f3f9c8836a913 to your computer and use it in GitHub Desktop.
Union type tuple destruction not working on TS
const returnError = async <R>(callback: (...args: any) => Promise<R>): Promise<[R] | [undefined, Error]> => {
try {
return await callback()
} catch (e) {
return e
}
}
const [result, err] = returnError(
() => api.call(/* ... */)
)
if (err instanceof Error) {
// . some logic and return / throw
}
result.someValue // Problem: TS will still show, that result can be undefined :(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment