`get`や`set`で表現できる。
```ts
class Human {
_name: string | undefined;
constructor(public id: number) {}
get doubleId(): number {
return this.id * 2;
}
set name(name: string | undefined) {
this._name = name + "!!!";
}
get name(): string | undefined {
return this._name;
}
}
const hori = new Human(100347);
hori.name = "hoge";
console.log(hori.name);
```
```console
$ npm run dev
hoge!!!
```
3つの特徴。
- `get`しか存在しない場合、その[[プロパティ (TypeScript)|プロパティ]]は[[読み取り専用プロパティ (TypeScript)|読み取り専用プロパティ]]になる
- `set`のパラメータが指定されていないとき、`get`のそれから推測される
- `get`と`set`は[[Member Visibility (TypeScript)|Member Visibility]]が等しくなくてはならない
[[TypeScript 4.3]]からは`get`と`set`の値シグニチャが異なる場合も動くようになった。