TypeScript 编译器允许调用不存在的 getter。这是编译器中的错误吗?

TypeScript compiler allows calling non-existing getter. Is this a bug in the compiler?

简而言之,我很困惑为什么这段代码编译没有错误:

class Foobar {
    #data = 0
    set data(val: number) { this.#data = val }
    // (Notice there is no getter for "data")
}
let foobar = new Foobar()
// Call the setter for data, no problemo
foobar.data = 123
// Call the getter for data, compiler should not allow this imo
console.log(foobar.data) // Prints "undefined"
  1. 这是打字稿编译器中的错误吗?我找不到任何地方提到这个问题。或者我的示例是否按预期工作(我错过了什么吗?)。
  2. 有什么方法(使用 eslint 规则或 tsc)来捕捉这个错误代码吗?

有一个 related but not exactly the same issue in TypeScript github repo 答案是这是定义的 ES6 class 行为。

有一个 linter rule 来检查你有没有 getter 的 setter 或没有 getter 的 setter。 我不知道是否有任何规则检查调用不存在的 getter。但是你可以定义一个抛出错误的 getter,这样至少在运行时你会看到错误并且单元测试会失败。