Last updated: Feb 29, 2024
Reading time·2 min

To solve the error "Accessors are only available when targeting ECMAScript 5
and higher", set the target property to es6 in your tsconfig.json file or
use the --target es6 flag when running the tsc command.

Here is an example of how the error occurs.
class Employee { private _salary = 0; // ⛔️ Error: Accessors are only available when targeting ECMAScript 5 and higher.ts(1056) get salary() { return this._salary; } // ⛔️ Accessors are only available when targeting ECMAScript 5 and higher.ts(1056) set salary(salary: number) { this._salary = salary; } }
target property to es6 in tsconfig.jsonTo solve the error, set the target property to es6 in your
tsconfig.json file.
{ "compilerOptions": { "target": "es6", // ... your other options } }
The target option changes which JavaScript features are down-leveled and which are left intact.
ES6 is a good choice because modern browsers support all ES6 features.
target option with a flagYou can also set the target option with a flag when using the
tsc command.
tsc src/index.ts --target es6
The target option changes which JS features are down-leveled and which aren't.
For example, if you use arrow functions and target ES5 or lower, your transpiled
code will use a function expression.
This often makes your code less performant and more verbose, however, it is able to run in much older browsers.
If you get an error that tsc is not recognized or not found, click on the link and follow the instructions.
You can learn more about the related topics by checking out the following tutorials: