I find it incredibly perplexing that this code is able to compile without any errors at all:
class Foobar {
#data = 0
set data(val: number) { this.#data = val }
// (There is no getter for "data")
}
let foobar = new Foobar()
// Set the value of data using the setter, works fine
foobar.data = 123
// Attempt to retrieve the value of data using the getter, shouldn't work according to my understanding
console.log(foobar.data) // Outputs "undefined"
- Could this be a flaw in the TypeScript compiler? I couldn't find any information about this issue, so maybe I overlooked something.
- Is there a tool like ESLint or tsc that can detect and prevent this kind of incorrect code?