TS1068
error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
Broken Code ❌
1 2 3 4 5 6 7 8
| class User { constructor(private firstName: string, private lastName: string) { }
function getName() { return `${this.firstName} ${this.lastName}`; } }
|
Fixed Code ✔️
Functions that are part of a class are being called “method”. The method of a class is defined without the function
keyword:
1 2 3 4 5 6 7 8
| class User { constructor(private firstName: string, private lastName: string) { }
getName() { return `${this.firstName} ${this.lastName}`; } }
|