Accessors are only available when targeting ECMAScript 5 and higher

avatar
Borislav Hadzhiev

Last updated: Feb 29, 2024
2 min

banner

# 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; } }
The code for this article is available on GitHub

# Set the target property to es6 in tsconfig.json

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 down-leveled 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.

The code for this article is available on GitHub

# Setting the target option with a flag

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 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.

The code for this article is available on GitHub

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.