I found it quite surprising that this piece of code compiles without any issues in TypeScript, but throws an error at runtime:
class Y {
set writeOnlyProp(value: number) {
// perform some actions here
}
}
const y = new Y()
// runtime error: Cannot read properties of undefined
console.log(y.writeOnlyProp.toString())
I am aware that in newer versions of TS, it is possible to add a getter that explicitly returns undefined
, but that solution feels like a workaround. Is there a more appropriate way to type this? Or maybe a TSConfig strict*
flag that can handle it automatically?