Skip to content

Instantly share code, notes, and snippets.

@nykula
Last active February 14, 2018 00:24
Show Gist options
  • Save nykula/083cd75a76457169ec1a2db7e73012cf to your computer and use it in GitHub Desktop.
Save nykula/083cd75a76457169ec1a2db7e73012cf to your computer and use it in GitHub Desktop.
Node-style callback in TypeScript with Nullthrows
import Nullthrows from "@makepost/nullthrows";
class Post {
id = "";
}
interface ICallback<T> {
(error: Error, _?: undefined): void;
(_: undefined, post: T): void;
}
const callback: ICallback<Post> = (error?: Error, post?: Post) => {
if (error) {
console.error(error.message);
} else {
console.log(Nullthrows(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