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.
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
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.
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
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
implementsor mixins for more. - A derived constructor must call
super(...)before touchingthis(error TS17009). Code that does not usethismay 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 numericlength; the argument keeps its full type, solongest("a", "b")returnsstring, 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.
extendsin a conditional means assignability, sonever extends Xis true (andneverin a naked position yieldsnever). - Interfaces vs intersections. Extending an interface reports property conflicts immediately; an intersection
&combines silently and can producenever. - Runtime effect. Only class
extendsexists at runtime (it sets the prototype chain). The other uses are erased. - Class mixins use
extendson a function call result, such asclass A extends Timestamped(Base) {}.