I was surprised to find that the following code compiles fine in TypeScript and only errors at runtime:
class X {
set writeOnlyProp(value: number) {
// do some setting stuff
}
}
const x = new X()
// runtime error: Cannot read properties of undefined
console.log(x.writeOnlyProp.toString())
I know it’s possible in newer versions of TS to add a getter that explicitly returns undefined
, but that seems like a hack. Is there a better way of typing this? Or perhaps a TSConfig strict*
flag that handles it automatically?