Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Keywords and Declarations

TypeScript extends keyword

Reference for TypeScript extends: class inheritance, interface extension, generic constraints and conditional types, with rules, errors and examples.

extends appears in four different places in TypeScript, and each means "is a subtype of": a class extending another class, an interface extending other interfaces, a type parameter constrained to a type, and the test in a conditional type.

In every case the right-hand side describes a minimum shape. A derived class inherits the members of its base class, an extended interface gains the members of its parents, and a constrained type parameter must be assignable to the constraint.

TypeScript
class Dog extends Animal {}
interface Loud extends Named, Sized {}
function longest<T extends { length: number }>(a: T, b: T): T { return a; }
type IsString<T> = T extends string ? true : false;

Try it

TypeScript Class and interface extension

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Constraints and conditional types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

TypeScript
class Derived<T> extends Base<T> implements Iface { }
interface Child extends ParentA, ParentB<string> { }
function f<T extends Constraint>(x: T) { }
type Cond<T> = T extends U ? X : Y;
Use Meaning
class A extends B A inherits B's instance and static members. The expression after extends can be any expression that evaluates to a constructor (for mixins), but a plain identifier is typical.
interface A extends B, C A includes all members of B and C. Conflicting property types are an error.
T extends C Constrains T: every type argument must be assignable to C.
X extends Y ? A : B Conditional type: A if X is assignable to Y, otherwise B.

Edge cases

  • A class can extend only one class. Use implements or mixins for more.
  • A derived constructor must call super(...) before touching this (error TS17009). Code that does not use this may run before the call.
  • Overriding a method with an incompatible signature is error TS2416.
  • Interfaces can extend classes; they inherit the members, including private ones, so only that class or its subclasses can implement them.

Usage notes

  • Constraints are minimums. T extends { length: number } accepts strings, arrays and any object with a numeric length; the argument keeps its full type, so longest("a", "b") returns string, not { length: number }.
  • Distribution. In a conditional type with a naked type parameter, a union is split: IsString<string | number> is "yes" | "no". Wrap both sides in tuples ([T] extends [string]) to stop that.
  • Not the same as value equality. extends in a conditional means assignability, so never extends X is true (and never in a naked position yields never).
  • Interfaces vs intersections. Extending an interface reports property conflicts immediately; an intersection & combines silently and can produce never.
  • Runtime effect. Only class extends exists at runtime (it sets the prototype chain). The other uses are erased.
  • Class mixins use extends on a function call result, such as class A extends Timestamped(Base) {}.