Skip to content

Instantly share code, notes, and snippets.

@ThomasKruegl
Last active February 12, 2018 08:24
Show Gist options
  • Save ThomasKruegl/db64a799b2c411c007f05276884e8197 to your computer and use it in GitHub Desktop.
Save ThomasKruegl/db64a799b2c411c007f05276884e8197 to your computer and use it in GitHub Desktop.
async function thisGetsCalled(): Promise<string> {
throw new Error("bla");
}
function convert(error: any) {
throw new Error("different");
}
async function main() {
// this is not working, error TS2322: Type 'string | void' is not assignable to type 'string'.
// Type 'void' is not assignable to type 'string'.
const result: string = await thisGetsCalled().catch(convert);
// this is working, but i would have to declare let result2 before the try, to work with it later
try {
const result2: string = await thisGetsCalled();
} catch (error) {
convert(error);
}
// this is working (though i don't know why..) but i would prefer await style, there are more calls to follow.
thisGetsCalled()
.catch((error: any) => { throw new Error("different"); })
.then((result3: string) => console.log(result3));
}
@ThomasKruegl
Copy link
Author

solution: function convert(error: any): never { ... }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment