Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
theimo1221 opened this issue Aug 20, 2022 · 2 comments
Closed

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

theimo1221 opened this issue Aug 20, 2022 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@theimo1221
Copy link

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.

@fatcerberus
Copy link

Essentially a duplicate of #47581.

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Aug 23, 2022
@typescript-bot
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants