Created
February 14, 2018 00:15
-
-
Save nykula/2f3553447db72eb30248862088d98734 to your computer and use it in GitHub Desktop.
Node-style callback in TypeScript
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
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