Open this example in the interactive library
Source code
type Brand<T, Name extends string> = T & { readonly __brand: Name };
console.log(
((v: string) =>
/^[0-9a-f]{6}$/i.test(v) ? (v as Brand<string, "Hex6">) : null)("eeaa66"),
);
Sample output
eeaa66
