Last active
March 13, 2020 10:51
-
-
Save nin-jin/3f470ad9c9498d3efedc5565d40b31d8 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
type TestUserType = Assert< | |
typeof User.Value, | |
{ | |
readonly name: string | |
readonly birthday: Moment | |
readonly email: string & { Brand: "Email" } | undefined | |
} | |
> |
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
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