-
-
Save blemoine/41a0e4b6d76dabd3bffcb34a0d5072b8 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
declare class EmailTag { | |
private __kind: 'email' | |
} | |
type Email = string & EmailTag; | |
function isEmail(str: string): str is Email { | |
//Very simple validation | |
return str.indexOf('@') > 0; | |
} | |
type User = { | |
name: string, | |
email: Email, | |
} | |
function createUser(name: string, email: string): User | {error: string} { | |
if(isEmail(email)) { | |
return { name, email }; | |
} else { | |
return {error: `${email} is not a valid email` }; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment