TypeScript Reference ยท Classes, Enums and Decorators
TypeScript this types
Reference for the TypeScript polymorphic this type: return this from methods to build fluent APIs that keep the subclass type, plus this parameters and guards.
Inside a class, the type this means "the type of the object the method is called on", which may be a subclass. This is called the polymorphic this type. A method declared as (): this returns whatever concrete class it was invoked on, so chained calls keep the subclass's methods available.
Related uses are the this parameter (describing what this must be inside a function) and the guard this is T (narrowing the receiver). They are separate features that share a keyword.
class Builder { add(): this { return this; } }
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 C { method(): this; // polymorphic this return type guard(): this is Narrower; // type guard on the receiver } function f(this: Context, arg: T): R; // this parameter (erased at runtime)
| Form | Description |
|---|---|
: this |
The receiver's exact type. Only valid inside classes and interfaces. |
this is T |
A type predicate on the receiver; narrows the variable the method was called on. |
this: parameter |
A fake first parameter that declares the expected this. It does not take an argument and is removed from the output. |
ThisType<T> |
Utility marker (with noImplicitThis) that sets the this type inside object-literal methods. |
Edge cases:
thiscannot be used in static members' types, since statics have no instance.- A method returning
thiscannot return a new object of the same class;new C()has typeC, notthis. Cast it or returnCinstead. - With
noImplicitThis(part of--strict), an unannotatedthisin a standalone function is an error.
Usage notes
- Without
: this, a base-class chain method returns the base type and subclass methods vanish from the chain. The polymorphicthisfixes this at no runtime cost. - Builders, query objects and test helpers are the natural fit. Each step returns
this, so you can call them in any order. this is Tnarrows the receiver in both branches, so write it for state machines (isOpen()/isClosed()).- A
thisparameter is useful for callbacks that need a specific context, like event handlers in older APIs. - Immutable designs should return a new instance instead. TypeScript cannot express "a new instance of the same subclass" without extra generics.
Related
- Methods, Inheritance, Mixins, User-defined type guards
- Lessons: this in functions, Classes