Error Message: "An interface can only extend an object type or intersection of object types with statically known members"

Typescript

Typescript Problem Overview


The following code:

export type Partial2DPoint = { x: number } | { y: number }
export interface Partial3DPoint extends Partial2DPoint {
  z: number
}

Fails with the following error:

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

Why is this happening?

Typescript Solutions


Solution 1 - Typescript

Types vs Interfaces differ in the ability to extend union types

(I'm answering my own question) This is because you cannot extend a union type using an interface. You must use type alias:

export type Partial2DPoint = { x: number } | { y: number }
export type Partial3DPoint = Partial2DPoint & { z: number }

See this answer:

> A class can implement an interface or type alias, both in the same exact way. Note however that a class and interface are considered static blueprints. Therefore, they can not implement / extend a type alias that names a union type.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionchibicodeView Question on Stackoverflow
Solution 1 - TypescriptchibicodeView Answer on Stackoverflow