This-types
this as a type means "the type of the current instance," including subclasses. A method that returns this stays fluent: a subclass call returns the subclass, not the base.
Detailed Explanation
Inside a class, this the value is the instance. this the type is a special type parameter the compiler fills with the actual receiver. On User it is User. On a call through an Admin instance it is Admin.
The common use is a fluent setter:
class Builder {
url(value: string): this {
return this;
}
}
If ApiBuilder extends Builder, then new ApiBuilder().url("/x") is ApiBuilder, not Builder. Returning Builder would have lost the subclass members on the next call. That is why the return type is this and not the class name.
You can also annotate the fake this parameter of a function, the first parameter named this, which is erased:
function domain(this: { email: string }): string {
return this.email.split("@")[1] ?? "";
}
That function can only be called with a receiver that has email. domain() is an error. user.domain = domain; user.domain() is fine if user matches. This is how you type this in callbacks and in old prototype-style APIs. noImplicitThis errors when this would otherwise be any.
ThisType<T> is a utility that marks an object-literal context so methods inside it have a typed this. It appears in Vue-style options objects and in tsconfig noImplicitThis situations. It is a hint, not a runtime wrapper.
this types are not a substitute for generics in every case. A standalone function that should work on any { id: string } is generic, or takes an explicit argument. this is for methods and for functions designed to be installed as methods.
F-bounded patterns (T extends Comparable<T>) overlap: they pass the self type as a parameter. this is the lightweight version when you are already on a class. Both exist because "return the receiver" and "compare to another of the same class" are common and easy to type too widely.
The cost of returning this is that the public type of the method now mentions this, which some declaration emit and some documentation tools print oddly. The benefit is that fluent APIs and mixin-style classes keep their subclass type through a chain of calls.
Code Examples
1. Fluent API that preserves the subclass.
// 1. Fluent API that preserves the subclass.
class Builder {
protected href = "";
url(value: string): this {
this.href = value;
return this;
}
}
class RequestBuilder extends Builder {
method = "GET";
post(): this {
this.method = "POST";
return this;
}
}
const req = new RequestBuilder().url("/pay").post();
console.log(req.method, req["href"]); // POST /pay — href is protected; use a getter in real code2. An explicit this parameter, erased at emit.
// 2. An explicit this parameter, erased at emit.
function domain(this: { email: string }): string {
return this.email.split("@")[1] ?? "";
}
const user = { email: "ada@example.com", domain };
console.log(user.domain()); // example.com
// domain();
// Error: The 'this' context of type 'void' is not assignable...3. this in a type position for a compare method.
// 3. this in a type position for a compare method.
class Node {
constructor(public value: number) {}
eq(other: this): boolean {
return this.value === other.value;
}
}
class LabeledNode extends Node {
constructor(value: number, public label: string) {
super(value);
}
}
const a = new LabeledNode(1, "a");
const b = new LabeledNode(1, "b");
console.log(a.eq(b)); // true — other is LabeledNodeCommon Gotchas
A free function call drops `this`
`const fn = obj.method; fn()` is the classic JS bug. TypeScript catches it when `this` is typed and `noImplicitThis` or a `this` parameter is in play.
The `this` parameter must be the first parameter and is erased
it does not exist in the emitted JavaScript argument list.
`ThisType<T>` only affects object literal context checking
it will not change `this` inside a `class` body.