A presentation at React Day Berlin in December 2019 in Berlin, Germany by Robin Pokorny
import * as t from ‘io-ts’; const Email = t.brand( t.string, (s): s is t.Branded<string, EmailBrand> => isEmail(s), ‘Email’, ); type Email = t.TypeOf<typeof Email>;
Email.decode(”) Email.decode(‘r0b1n’) Email.decode(‘r@0b1n.at’) Email.decode(2020) Email.decode(null)
import * as E from ‘fp-ts/lib/Either’ pipe( Email.decode(anything), E.fold(onFail, onSuccess) )
const a: Email = ‘r@0b1n.at’ // -> Type ‘“r@0b1n.at”’ is not assignable to type ‘Branded<string, Email>’.
const SignUpForm = t.type({ company: String50, email: Email, password: Password, phone: option(Phone), sendNewsletter: t.boolean, }); const form = useForm( SignUpForm, initialState );
https://typescript.fun @estejs