Back to blog

Little Big Detail #2: Contextual Class Names

Little Big Details are the subtle details that quicktype takes care of when turning your data into beautiful, readable code. This Little Big Detail is about how quicktype uses the names of outer types to name inner types.

Little Big Details are the subtle details that quicktype takes care of when turning your data into beautiful, readable code.

This Little Big Detail is about how quicktype uses the names of outer types to name inner types.

The Problem

Consider this JSON:

{
  "user": {
    "address": {
      "street": "123 Main St",
      "city": "Springfield"
    }
  },
  "company": {
    "address": {
      "street": "456 Business Ave",
      "city": "Commerce City",
      "suite": "100"
    }
  }
}

Both user and company have an address, but they have different structures. A naive approach might name both Address and cause conflicts.

The Solution

quicktype uses contextual naming to create unique, meaningful names:

interface Root {
    user: User;
    company: Company;
}

interface User {
    address: UserAddress;
}

interface UserAddress {
    street: string;
    city: string;
}

interface Company {
    address: CompanyAddress;
}

interface CompanyAddress {
    street: string;
    city: string;
    suite: string;
}

How It Works

When quicktype encounters a nested type that needs a name, it:

  1. Looks at the property name (address)
  2. Looks at the parent type name (User or Company)
  3. Combines them if needed to avoid collisions
  4. Only adds the prefix when necessary (if there's no collision, it keeps the simple name)

This produces clean, readable code that avoids naming conflicts automatically.