Matching on Branded Types in TypeScript and Adding a Literal for Differentiation

I am curious to know if I can match on branded types?

Lets say I have the following schema:

const normalUserId = S.UUID.pipe(
  S.brand("normal-user-id"),
);

const specialUserId = S.UUID.pipe(
  S.brand("normal-user-id"),
);

const foundUser = S.Struct({
   id: S.Union(normalUserId, specialUserId)
});

switch (foundUser.id) {
  // No way to match on the branded type
}


And from what I understand, the branded type is merely a typescript thing. It does not carry any other kind of value to differentiate the type? i.e. { _tag: 'normal-user-id', value: '<uuid>' }

Am I correct?

If I wanted to match on the user type, I'd need to add in a literal to the foundUser type, correct?

const foundUser = S.Struct({
   id: S.Union(normalUserId, specialUserId),
   type: S.Union(S.Literal('special'), S.Literal('normal'))
});

switch (foundUser.type) { 
   case 'special': { ... do something for special user }
   case 'normal': { ... do something for normal user }
} 
Was this page helpful?