Typescript: Adding a compilerOption to disable error on property override accessor in 4.0.beta

Created on 3 Jul 2020  路  1Comment  路  Source: microsoft/TypeScript

Search Terms

property override accessor

Suggestion

Previously in 3.9, property can override an accessor with no emit errors. And in 4.0 beta, a break change was introduced by #33509.

It's reasonable to report an error on property override accessor in most case, but I'm using experimentalDecorators to inject a property accessor in prototype.

Currently I cannot find a solution for this use case, so I'm suggesting to add a new compilerOption to disable this check(strictOnly?) for compatibility.

Use Cases

class Animal {
    private _age: number
    get age(){ return this._age }
    set age(value){ this._age = value }
}

class Dog extends Animal {
    @defaultValue(100) age: number;     // Unexpected error here
}

function defaultValue(value) {
    return (obj, name) => {
        Object.defineProperty(obj, name, {
            get() { return value }
        })
    }
}

Checklist

My suggestion meets these guidelines:

  • [ ] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [x] This wouldn't change the runtime behavior of existing JavaScript code
  • [x] This could be implemented without emitting different JS based on the types of the expressions
  • [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • [x] This feature would agree with the rest of TypeScript's Design Goals.
Awaiting More Feedback Suggestion

Most helpful comment

I agree. Some old version .d.ts files for 3rd-party library may contain unprecise definitions, such as declaring accessors as properties, for example, PIXI.js v4.x:

class Sprite extends Container {
...
  width: number; // <- in fact accessor here
  height: number; // <- in fact accessor here
...
}

So extending this class will cause an error:

class MySprite extends PIXI.Sprite {
  /** @override */
  get width(): number {
    // 'width' is defined as a property in class 'Sprite', but is overridden here in 'MySprite' as an accessor.
  }
}

Since changing all these legacy definition files is a lot of works, a compiler option will be of much help.

>All comments

I agree. Some old version .d.ts files for 3rd-party library may contain unprecise definitions, such as declaring accessors as properties, for example, PIXI.js v4.x:

class Sprite extends Container {
...
  width: number; // <- in fact accessor here
  height: number; // <- in fact accessor here
...
}

So extending this class will cause an error:

class MySprite extends PIXI.Sprite {
  /** @override */
  get width(): number {
    // 'width' is defined as a property in class 'Sprite', but is overridden here in 'MySprite' as an accessor.
  }
}

Since changing all these legacy definition files is a lot of works, a compiler option will be of much help.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RyanCavanaugh picture RyanCavanaugh  路  205Comments

xealot picture xealot  路  150Comments

born2net picture born2net  路  150Comments

jonathandturner picture jonathandturner  路  147Comments

disshishkov picture disshishkov  路  224Comments