JS Built-ins
TypeScriptintermediate

Interface β€” Opaque/Brand Type

πŸ“– Usage Example
type UserId = string & { readonly __brand: 'UserId' }
function createUserId(id: string): UserId {
  return id as UserId
}

A branded type uses an intersection with a phantom property to distinguish nominally equal types.

Brand / Opaque Types

A branded type adds a phantom property to distinguish types that share the same underlying representation.

Challenge: Implement createUserId(id) that casts a string to UserId.

type UserId = string & { __brand: 'UserId' }
createUserId('user-1')  // β†’ 'user-1' (as UserId)
πŸ’‘ Hints (1)
  • β€’ At runtime, a branded type is just the underlying primitive. The brand only exists at the type level.