Typescript: Lazy getter for static property produce error

Created on 18 Sep 2019  ·  6Comments  ·  Source: microsoft/TypeScript

TypeScript Version: 3.6.3

Code

class A {
    static get callbacks(): any[] {
        delete this.callbacks;
        return this.callbacks = []
    }
}

Expected behavior:
No error should be shown
image

Actual behavior:
error TS2704: The operand of a delete operator cannot be a read-only property.

Playground Link: playground

Working as Intended

All 6 comments

Current workaround

class A {
    static get foo(this: any) {
        delete this.foo;
        return this.foo = []
    }
}

This behavior was requested; see #11730

@RyanCavanaugh i think error should be thrown only outside of getter body. Or on explicit readonly specifier. But inside getter - not

It's common case for lazy getter.
i.e. look at example on MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get#Smart_self-overwriting_lazy_getters

get notifier() {
  delete this.notifier;
  return this.notifier = document.getElementById('bookmarked-notification-anchor');
},

I'm skeptical on "common" given no one else reporting this in the prior 30,000 issues. This is a good use case for // @ts-ignore.

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Was this page helpful?
0 / 5 - 0 ratings