Last active
February 14, 2018 00:24
-
-
Save nykula/083cd75a76457169ec1a2db7e73012cf to your computer and use it in GitHub Desktop.
Node-style callback in TypeScript with Nullthrows
This file contains 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
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