Do you already know about auto-accessors in TypeScript?

technology1 min

byLucas Santos

This page was machine translated. Read original / Suggest a fix

TypeScript has a bunch of really interesting features that few people know about, one of them is the use of auto-accessors in classes.

Around since TypeScript 4.9, it’s also a feature described in the original decorators proposal.

Essentially, this syntax works like this: when we have an accessor, we usually have a get method that returns an internal variable of a class, for example:

class Pessoa {
#__nome: string;
get name() {
return this.#__nome;
}
set name(val: string) {
this.#__nome = val;
}
constructor(nome: string) {
this.nome = nome;
}
}

Notice how we have not one, but 7 lines just to create an accessor that sets and gets the internal variable #__nome? It would be much better if we could do all of that at once, and that’s exactly why auto-accessors exist. Everything I wrote above can become this:

class Person {
accessor nome: string;
constructor(nome: string) {
this.nome = nome;
}
}

Under the hood, what auto-accessors do is exactly what we did in the first snippet, they expand nome into a private internal class variable, and an external variable accessible only through a getter and a setter.

In general, this feature isn’t something very related to logic, but rather to quality of life, especially when we’re creating decorators that need several getters and setters.

Footnotes#

  1. We already talked about decorators here. ↩