Complete worked example
typescript
declare const roleBrand: unique symbol;
type RoleId = string & { readonly [roleBrand]: true };
function roleId(value: string): RoleId | undefined {
return /^role_[a-z0-9]+$/.test(value) ? value as RoleId : undefined;
}
const id = roleId("role_a1");- Reasoning
- The assertion is confined to the constructor after a concrete syntax check.