Skip to content
webtype.orgFlatten an intersection

    ↑↓ move · ⏎ open · esc close

    Patterns

    How do I turn `{ a: string } & { b: number }` into `{ a: string; b: number }` so tooltips are readable and equality checks pass?

    A homomorphic mapped type walks every key once and rebuilds a single object. Modifiers survive; the intersection does not.

    The recipe

    type Prettify<T> = { [K in keyof T]: T[K] }
    
    type Base = { a: string }
    type Extra = { b?: number; readonly c: boolean }
    
    type Merged = Prettify<Base & Extra>

    The build compiles this and checks each result below.

    How it works

    1. 01
      type Prettify<T> = { [K in keyof T]: T[K] }

      It looks like it does nothing, and for a plain object it does. Handed an intersection it walks the union of both key sets and emits one object containing all of them.

    What you get

    Where it goes wrong

    It flattens one level. An intersection nested inside a property stays an intersection, and applying this recursively will also expand every built-in type it touches — including `Date` and `Promise`, which is rarely what anyone wanted.

    Takeaway

    Intersections assemble an object type; a homomorphic mapping finishes one. Assembling without finishing is the most common reason a correct-looking type fails an equality check.

    See also

    Patterns