TS2663

error TS2663: Cannot find name ‘firstName‘. Did you mean the instance member ‘this.firstName‘?

Broken Code ❌

1
2
3
4
5
class Person {
get firstName(): string {
return firstName;
}
}

Fixed Code ✔️

If you want to use a getter, you need to back it up with a private property:

1
2
3
4
5
6
7
class Person {
private _firstName: string = '';

get firstName(): string {
return this._firstName;
}
}

Starting from TypeScript 4.9, you can also use an auto-accessor field:

1
2
3
class Person {
accessor firstName: string = '';
}