From String type to Email type

I am researching String type. The question is - can i make special types from string type in F# ? Can it accomplished in F# or any other langs. Example. I am modelling domain with types and i have User type ( typescript).

type User = {
  name: string;
  age: number;
  email: Email;
  password: Password

type Email = /some pattern here/
I want Email type here because i need some shape of the string. So any string too general for this. So the shape is.

  • It must have only 2 special chars . and @.
  • It must have only a-z chars and length of it not more then 100 chars.
    Can i accomplish this in F# ? I can not do this in typescript for example.
    I read about refinement type it’s pretty close to that i need.

Take a look at Designing with types: Constrained strings article.

1 Like