Menu

TypeScript extends: Extend an Interface or a Type

The extends keyword builds one type from another. Learn how to extend an interface (once or from several), extend a type alias with &, override a property type, replace properties with Omit, and what extends means in classes, generic constraints and conditional types.

This page includes runnable editors - edit, run, and see output instantly.

interface Child extends Parent creates a new interface with every member of Parent plus its own. Values of the child type can be used wherever the parent is expected, because they have all of its properties.

The same keyword appears in four places in TypeScript, with related but different meanings:

WhereExampleMeaning
Interfaceinterface Dog extends Animal {}inherit the parent's members
Classclass Dog extends Animal {}inherit fields and method implementations (runtime JavaScript)
Generic parameter<T extends Animal>constraint: the type argument must be assignable to Animal
Conditional typeT extends string ? A : Btest: is T assignable to string?

Extending Multiple Interfaces

List several parents after extends, separated by commas. The result has the members of all of them:

interface Identified {
  id: number;
}

interface Timestamped {
  createdAt: Date;
  updatedAt: Date;
}

interface Post extends Identified, Timestamped {
  title: string;
}

// Post requires id, createdAt, updatedAt and title

If two parents declare the same property with types that are not identical, the child is error TS2320 (Interface 'Post' cannot simultaneously extend types 'A' and 'B'), unless the child redeclares that property with a type assignable to both.

Extending a Type Alias

A type alias has no extends clause. You build on it with an intersection, &, which combines the members of both sides. An interface can also extend an object type alias directly, and a type alias can intersect an interface: the two kinds mix freely.

The two routes behave the same until properties conflict: extends reports a conflicting property at the declaration, while & silently turns it into never. The comparison is on the interface vs type page, and the details of & are on the intersection types page.

Overriding Property Types

A child interface may redeclare a parent property, but only with a type that is assignable to the parent's. Narrowing string to a literal, or removing null from a union, is allowed. Changing the type to something unrelated is not:

index.ts(13,11): error TS2430: Interface 'Label' incorrectly extends interface 'Shape'.
  Types of property 'kind' are incompatible.
    Type 'number' is not assignable to type 'string'.

The rule exists so that a Label could still be used everywhere a Shape is expected. If kind could be a number, code written for Shape that calls shape.kind.toUpperCase() would break.

Replacing Properties with Omit

When you really need a different type for an inherited property, remove it from the parent first with Omit, then declare the new one. A common case is converting API data, where ids and dates arrive as strings:

Omit<ApiUser, "id" | "createdAt"> is { name: string }, so the new id and createdAt do not conflict with anything. Pick, Partial and the other utility types can be extended the same way.

extends with Classes

class Dog extends Animal is JavaScript class inheritance, and unlike interface extends it exists at runtime: Dog gets Animal's constructor, fields and method implementations, and new Dog(...) instanceof Animal is true. TypeScript adds type checks on top, such as the override keyword:

A class can extends only one class, but it can implements any number of interfaces. implements copies nothing: it only checks that the class declares what the interface requires. super, protected members and override are covered in inheritance.

extends in Generics and Conditional Types

Inside angle brackets, extends is a constraint. <T extends { length: number }> accepts any type argument that has a numeric length, and lets the function body use .length. In a conditional type, T extends U ? X : Y asks whether T is assignable to U.

In both places extends means "is assignable to", the same relation the interface form checks. Constraints are covered in depth in generic constraints, and type-level tests in conditional types.

Frequently Asked Questions

How do you extend an interface in TypeScript?

Write interface Child extends Parent { ... }. The child has every property of the parent plus the ones it declares. To extend several interfaces at once, separate them with commas: interface C extends A, B { ... }.

How do you extend a type alias in TypeScript?

A type alias cannot use extends; combine types with an intersection instead: type Admin = User & { permissions: string[] }. An interface can also extend an object type alias directly: interface Admin extends User { permissions: string[] }.

Can I override a property type when extending an interface?

Only with a compatible, narrower type: kind: "circle" can replace kind: string. A different type, like number for a string property, is error TS2430 (Interface 'X' incorrectly extends interface 'Y'). To replace a property with an unrelated type, remove it first: interface User extends Omit<ApiUser, "id"> { id: number }.

What is the difference between extends and implements?

extends inherits: an interface gets the parent's members, and a class gets the parent class's fields and method implementations. implements only checks: a class that implements an interface must declare every member itself, and gains nothing from the interface.

What does extends mean in a generic like <T extends string>?

It is a constraint, not inheritance. T extends string means the type argument must be assignable to string, so inside the function you can use string methods on T. In a conditional type, T extends U ? X : Y is a test: if T is assignable to U, the result is X, otherwise Y.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED