Skip to content

Instantly share code, notes, and snippets.

@blemoine
Created March 31, 2018 20:36
Show Gist options
  • Save blemoine/41a0e4b6d76dabd3bffcb34a0d5072b8 to your computer and use it in GitHub Desktop.
Save blemoine/41a0e4b6d76dabd3bffcb34a0d5072b8 to your computer and use it in GitHub Desktop.
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