Skip to content

Override Setter only --> Getter undefined but no compiler error #50383

Closed
@theimo1221

Description

@theimo1221

Bug Report

When a subclass overrides a setter of the parent but not it's getter any call to the getter will result in undefined.
But there is no compiler error, even though strict checks are activated,
So during development it seems like the getter is accessible, while at runtime it is not.

🔎 Search Terms

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about setter overide

⏯ Playground Link

Playground link with relevant code

💻 Code

class Vehicel {
    protected _wheels: number = 4;

    public get wheels(): number {
        return this._wheels;
    }

    public set wheels(value: number) {
        this._wheels = value;
    }
}

class Motorcycle extends Vehicel {
    constructor() {
        super()
        this._wheels = 2
    }

    public override set wheels(value: number) {
        if(value > 3) {
            console.log("More than 3 wheels? This is no motorcycle.")
            return;
        }
        this._wheels = value
    }
}

var a = new Motorcycle()
var b = new Vehicel()

console.log(`The Vehicel has ${b.wheels} wheels`)
console.log(`The Motorcycle has ${a.wheels} wheels`) // wheels is undefined here

🙁 Actual behavior

Using the getter is possible without warning/error and returns undefined

🙂 Expected behavior

To ensure type safety, the developer should receive a warning/error when he tries to use a getter, which is undefined as a result of overriding the setter. Alternatively the developer should receive an error on the subclass stating Missing Override for getter xyz, due to setter override being declared.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions