Skip to content
Tutorials Center
My learning

Topics

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.

TypeScript
class Builder {
  add(): this { return this; }
}
function describe(this: { name: string }) { return this.name; }

Try it

TypeScript The polymorphic this type

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 this parameters and lost this

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
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: User without a receiver: error TS2684 ("The 'this' context of type 'void' is not assignable to method's 'this' of type 'User'").
  • Using this in a function with no declared type: error TS2683 under noImplicitThis.
  • A static member cannot use the this type; this inside 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.method as a callback detaches it. Use bind, an arrow wrapper or an arrow field. With strictBindCallApply, bind, call and apply check their arguments against the this parameter.
  • 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.
  • ThisParameterType and OmitThisParameter extract or remove the explicit this parameter of a function type.
  • ThisType<T> is a marker interface used in object literal methods (Vue 2 style options objects) to set this without a parameter.
  • Interfaces may use this types in method signatures to express the same chaining.