Skip to content
TS2312

    ↑↓ move · ⏎ open · esc close

    TS2312

    Interface cannot extend a union

    An interface can only extend an object type or intersection of object types with statically known members.

    The compiler’s own words. Not translated — this is the string you pasted into a search box.

    An interface tried to inherit from a union whose members are not all known at once.

    Reproduction

    type Choice = { a: 1 } | { b: 2 }
    
    interface Both extends Choice {
      c: 3
    }

    The build asserts this emits exactly this code.

    Why the compiler says this

    Interface inheritance merges a definite set of object members. A union means one branch or another, so there is no single member list for the interface declaration to inherit and merge.

    Fixes

    1. 01
      type Choice = { a: 1 } | { b: 2 }
      
      type WithC = Choice & { c: 3 }

      Use an intersection alias to add members to every branch of a union.

    2. 02
      interface Base {
        a: number
      }
      
      interface Extended extends Base {
        c: number
      }

      Extend an interface or object type when the base really has a fixed shape.

    Takeaway

    Interfaces extend known object shapes. Use type aliases to compose unions and intersections.

    Where to go next

    Errors