TS2680

error TS2680: A ‘this’ parameter must be the first parameter.

Broken Code ❌

1
2
3
4
5
6
7
type Person = {
name: string;
}

export function sayHello(text: string = 'Hello', this: Person): void {
console.log(`${text} ${this.name}`)
}

Fixed Code ✔️

TypeScript requires that a this parameter always comes first in the list of parameters:

1
2
3
4
5
6
7
type Person = {
name: string;
}

export function sayHello(this: Person, text: string = 'Hello'): void {
console.log(`${text} ${this.name}`);
}