TypeScript Reference ยท Keywords and Declarations
TypeScript this keyword, this type and this parameter
Reference for this in TypeScript: the polymorphic this type, explicit this parameters, noImplicitThis, arrow functions and typing callbacks that rely on this.
In TypeScript, this shows up in three forms. The value this works as in JavaScript, and its type depends on where you are. The polymorphic this type, used as a return type in classes, means "the type of the object this method was called on". And a fake first parameter named this declares what a plain function expects to be bound to.
With noImplicitThis (part of strict), the compiler reports any use of this whose type would be an implicit any, which finds the classic bug of losing this in a callback.
class Builder { add(): this { return this; } } function describe(this: { name: string }) { return this.name; }
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
method(): this { return this; } // polymorphic this type function f(this: T, a: A): R { /* ... */ } // explicit this parameter const g = (): void => { this; }; // arrow: this comes from the outside isFoo(): this is Foo { return true; } // this-based type guard
| Form | Description |
|---|---|
this (value) |
In a method, the instance. In a plain function with no this parameter, implicitly any (an error under noImplicitThis). In a top-level ES module it is undefined. |
this type |
Only valid inside classes and interfaces. Resolves to the type of the receiver, so subclasses keep fluent chains. |
this: T parameter |
Not a real parameter: it is erased and does not count toward arity. Callers must supply a matching receiver. |
this is T |
A type predicate on the receiver, for narrowing x.isFoo() calls. |
Errors
- Calling a function that declares
this: Userwithout a receiver: error TS2684 ("The 'this' context of type 'void' is not assignable to method's 'this' of type 'User'"). - Using
thisin a function with no declared type: error TS2683 undernoImplicitThis. - A static member cannot use the
thistype;thisinside a static method refers to the constructor.
Usage notes
- Arrow functions do not have their own
this; they capture the enclosing one. That is why class fields written as arrow functions (onClick = () => ...) are safe to pass as callbacks, at the cost of one function per instance. - Lost receivers. Passing
obj.methodas a callback detaches it. Usebind, an arrow wrapper or an arrow field. WithstrictBindCallApply,bind,callandapplycheck their arguments against thethisparameter. - Method chaining. Return
this(the type), not the class name, so subclass methods stay available after a base-class call. - Callbacks that set
this. Declare it on the callback type, for example(this: HTMLElement, ev: Event) => void, as DOM typings do. ThisParameterTypeandOmitThisParameterextract or remove the explicitthisparameter of a function type.ThisType<T>is a marker interface used in object literal methods (Vue 2 style options objects) to setthiswithout a parameter.- Interfaces may use
thistypes in method signatures to express the same chaining.
Related
- class, super, this parameter, ThisParameterType, noImplicitThis
- Lessons: this in functions, Classes