Created
May 31, 2022 05:34
-
-
Save ehatricksmith/eba54330a19e463e591315f59f53a25d to your computer and use it in GitHub Desktop.
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
function parseUserId(userId: string): UserId { | |
if (!userId.startsWith("__special__prefix")) { | |
throw new Error("This is not a userId!"); | |
} | |
return userId as UserId; | |
} | |
// The happy path | |
const user: User = { id: parseUserId("__special__prefix:abcd") }; | |
// Fails at compile-time | |
const user: User = { id: "abcd" }; | |
// Throws an error at runtime | |
const user: User = { id: parseUserId("abcd") }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment