#2432·orval

Generated branded types for OpenAPI aliases

Author: SouthclawsCreated Oct 9, 2025Updated Sep 15, 2026
Labelsenhancement

Something I make use of in OpenAPI and other languages (including TypeScript) is distinct invariant types, or distinct aliases. Common in type systems with nominative typing. It's a little harder in TypeScript as it uses structural types so any supertype of string is equal to any other supertype of string.

This is useful for distinguishing between semantically different types, such as IDs vs Slugs in a web application, an excerpt from Storyden's OpenAPI schema which uses Orval for the frontend:

yaml
    Identifier:
      type: string
      format: xid
      x-go-type: string
      example: "cc5lnd2s1s4652adtu50"
      description: A unique identifier for this resource.

    Mark:
      x-go-type: string
      type: string
      format: xid-prefixed-kebab-case-string
      example: "cc5lnd2s1s4652adtu50-top-10-movies-thread"

In the backend, this distinctly generates a type for Identifiers and another type for Marks so that mistakes are hard to make when calling functions that expect either an ID or a Mark/slug. On the frontend, this generates:

typescript
type Identifier = string
type Mark = string

TypeScript does have a couple of tricks for "branding" types to implement this functionality and prevent calling some API with an ID when you actually need a slug, or vice versa.

typescript
declare const __brand: unique symbol
type Brand<B> = { [__brand]: B }
export type Branded<T, B> = T & Brand<B>

It would be really nice if Orval had an option to generate these supertypes of primitives (any component of the schema that's a primitive string, number, integer, etc) somehow. Either via a global flag or an x- property on the schema.