Skip to content

Instantly share code, notes, and snippets.

@nykula
Created February 14, 2018 00:15
Show Gist options
  • Save nykula/2f3553447db72eb30248862088d98734 to your computer and use it in GitHub Desktop.
Save nykula/2f3553447db72eb30248862088d98734 to your computer and use it in GitHub Desktop.
Node-style callback in TypeScript
class Post {
id = "";
}
interface ICallback<T> {
(error: Error, _?: any): void;
(_: any, post: T): void;
}
const callback: ICallback<Post> = (error: Error, post: Post) => {
if (error) {
console.error(error.message);
} else {
console.log(post.id);
}
};
callback(undefined, new Post());
callback(new Error("Unauthorized"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment