Created
October 22, 2021 10:28
-
-
Save Dionid/d67aa910c47f255bd70f3f9c8836a913 to your computer and use it in GitHub Desktop.
Union type tuple destruction not working on TS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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