Skip to content

Instantly share code, notes, and snippets.

@nin-jin
Last active March 13, 2020 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nin-jin/3f470ad9c9498d3efedc5565d40b31d8 to your computer and use it in GitHub Desktop.
Save nin-jin/3f470ad9c9498d3efedc5565d40b31d8 to your computer and use it in GitHub Desktop.
Статикодинамические валидаторы
type TestUserType = Assert<
typeof User.Value,
{
readonly name: string
readonly birthday: Moment
readonly email: string & { Brand: "Email" } | undefined
}
>
const Email = Brand({
Email : Pipe( Str , Check( input => /@/.test( input ) ) )
})
const User = Rec({
name : Str ,
birthday : Pipe( Str , Moment ) ,
email : Maybe( Email ) ,
})
function sayHello( user : typeof User.Value ) {
console.log( 'Hello' , user.name )
}
function sendEmail( email : typeof Email.Value ) {
console.log( 'Send to' , email )
}
const alice = User({
name : 'Alice' ,
birthday : '2000-01-01T12:00:00Z' ,
})
sayHello( alice )
if( alice.email ) sendEmail( alice.email )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment