Skip to content

Instantly share code, notes, and snippets.

@ehatricksmith
Created May 31, 2022 05:34
Show Gist options
  • Save ehatricksmith/eba54330a19e463e591315f59f53a25d to your computer and use it in GitHub Desktop.
Save ehatricksmith/eba54330a19e463e591315f59f53a25d to your computer and use it in GitHub Desktop.
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