Accessors are only available when targeting ECMAScript 5 and higher

avatar

Borislav Hadzhiev

Last updated: Mar 23, 2022

banner

Photo from Unsplash

Accessors are only available when targeting ECMAScript 5 and higher #

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.

accessors only available when targeting ecmascript

Here is an example of how the error occurs.

index.ts
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; } }

To solve the error, set the target property to es6 in your tsconfig.json file.

tsconfig.json
{ "compilerOptions": { "target": "es6", // ... your other options } }

The target option changes which JavaScript features are downleveled and which are left intact.

If this doesn't resolve the error, try restarting your IDE. VSCode often glitches and needs a reboot.

ES6 is a good choice, because modern browsers support all ES6 features.

You can also set the target option with a flag when using the tsc command.

shell
tsc src/index.ts --target es6

The target option changes which JS features are downleveled 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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.