Open this example in the interactive library
Source code
type Brand<T, Name extends string> = T & { readonly __brand: Name };
console.log(
((s: string): Brand<string, "Slug"> | null =>
/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(s) ? (s as Brand<string, "Slug">) : null)(
"job-queue",
),
);
Sample output
job-queue
