The right answer to this is probably a nominal-like type using branding as in @LionelRowe's answer; unless you're dealing with string literal types, so that these email address strings are hardcoded as string literals in your code, there's no way for the compiler to enforce such a constraint. The compiler will only see string
.
Even if the compiler knows the exact string literals you are specifying, such as const str = "[email protected]"
, it's beyond the abilities of TypeScript 4.0 to validate strings like this.
The amazing thing is that it will not be beyond the abilities of TypeScript 4.1, which will introduce template literal types (as implemented in microsoft/TypeScript#40336)... which can actually do operations on string literals.
Unfortunately, as far as I can see, it will be at the edge of TypeScript's abilities. In order to get the compiler to validate that a string is no longer than (say) 32 characters and that it must contain only alphabetic characters, "."
, and "@"
, you'll have to use generics and probably recursive conditional types (also slated for TS4.1 as implemented in microsoft/TypeScript#40002). At every turn you will find yourself avoiding various recursion or combinatorics limits; it's easy to write something that breaks the compiler for strings of just 10 or so characters long. Even when you manage not to hit the limits, the compiler tends to be slow. Type an invalid email and wait 10 seconds before the compiler shows an error. It's a mess and I don't recommend it, at least in TS4.1.
But I love this stuff too much not to do it:
type Lower = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i' | 'j' | 'k' | 'l' | 'm' | 'n' | 'o' | 'p' | 'q' | 'r' | 's' | 't' | 'u' | 'v' | 'w' | 'x' | 'y' | 'z';
type AllowedChars = Lower | '.' | '@'
type RestrictedToChars<T extends string, A extends string, Y = T, N = never> =
string extends T ? N :
T extends `${infer F}${infer F}${infer F}${infer F}${infer F}${infer F}${infer R}` ?
[F] extends [A] ? RestrictedToChars<R, A, Y, N> : N :
T extends `${infer F}${infer F}${infer F}${infer R}` ?
[F] extends [A] ? RestrictedToChars<R, A, Y, N> : N :
T extends `${infer F}${infer R}` ?
[F] extends [A] ? RestrictedToChars<R, A, Y, N> : N :
Y
The type RestrictedToChars<T, A, Y, N>
takes a string literal T
and a union of allowed characters A
, and evaluates to: Y
if T
contains only characters in A
, or N
if T
contains any characters not in A
.
type SplitChars<T extends string> = string extends T ? string[] :
T extends `${infer F1}${infer F2}${infer F3}${infer F4}${infer F5}${infer F6}${infer R}` ? [F1, F2, F3, F4, F5, F6, ...SplitChars<R>] :
T extends `${infer F1}${infer F2}${infer F3}${infer R}` ? [F1, F2, F3, ...SplitChars<R>] :
T extends `${infer F1}${infer R}` ? [F1, ...SplitChars<R>] :
[]
type CheckMaxLength<T extends string, L extends number, Y = T, N = never> =
SplitChars<T>[L] extends undefined ? Y : N;
The type CheckMaxLength<T, L, Y, N>
takes a string literal T
and a maximum numeric length L
, and evaluates to: Y
if T
is at most L
characters long, or N
if T
is more than L
characters long.
You can then define Email<T>
as a generic validator on a string literal type T
, and make User<T>
generic the same way:
type Email<T extends string> = T &
CheckMaxLength<T, 32, RestrictedToChars<
`${lowercase T}`, AllowedChars, T, ["Email can only contain alphabet or @ or ."]
>, ["Email needs to be 32 characters or less"]>;
type User<T extends string> = {
name: string;
email: Email<T>;
}
Finally you can see the compiler do the validation:
const user = <T extends string>(user: User<T>) => user;
user({ name: "Dave Morrison", email: "[email protected]" }); // okay
user({ name: "Van Morrison", email: "[email protected]" }); // error!
// ------------------------> ~~~~~
// '["Email can only contain alphabet or @ or ."]'
user({ name: "Jim Morrison", email: "[email protected]" }); // error!
// ------------------------> ~~~~~
// '["Email needs to be 32 characters or less"]'
Exquisitely awful! Again, I do not recommend that you use this. But when TS4.1 is released, it will be technically possible to have the compiler validate strings this way.
Playground link
brand
alternative to refinement. Take a look at this issue for example usage: github.com/gcanti/io-ts/issues/265