TS2683

error TS2683: ‘this’ implicitly has type ‘any’ because it does not have a type annotation.

Broken Code ❌

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

Fixed Code ✔️

This bug is also a consequence of TS2680. To fix the bug we have to define the context of our function. It can be done by defining this as the first parameter in our argument list and giving it a type annotation:

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}`);
}