Skip to content

Instantly share code, notes, and snippets.

@jswhisperer
Last active June 5, 2025 17:28
Show Gist options
  • Select an option

  • Save jswhisperer/8d8b319fe8b1b8f17c16f286b57ef60f to your computer and use it in GitHub Desktop.

Select an option

Save jswhisperer/8d8b319fe8b1b8f17c16f286b57ef60f to your computer and use it in GitHub Desktop.
typed try catch
type TryCatchProps<T> = {
tryFn: () => T;
catchFn: (error: unknown) => T;
}
function tryCatch<T>({ tryFn, catchFn }: TryCatchProps<T>): T {
try {
return tryFn();
} catch (error) {
return catchFn(error);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment