question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

A derived class is allowed be created without a parent's getter or setter.

See original GitHub issue

Bug Report

🔎 Search Terms

class extend override getter setter accessor

🕗 Version & Regression Information

  • This seems to occur on all versions on the Playground (3.9+)

⏯ Playground Link

Playground link with relevant code

💻 Code

class Parent {
    public get f() { return () => {}; }
}

class Child extends Parent {
    public set f(func: () => void) { }
}

const c = new Child();
c.f(); // runtime error because Child doesn't implement the getter for f

🙁 Actual behavior

Class Child is allowed to extend class Parent even though, by adding a setter for f, it discards the getter for f and therefore doesn’t implement Parent’s contract.

🙂 Expected behavior

TypeScript should emit an error because Child is not compatible with Parent.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:5
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
OwenDelahoycommented, Feb 8, 2022

In the following code, typescript expects a boolean, however at runtime it is actually undefined.

Playground Link

class Parent {
    protected _value = false;
    public get value() { return this._value; }
}

class Child extends Parent {
    public set value(value: boolean) { this._value = value; }
}

function f(): boolean {
    const obj = new Child();
    return obj.value;
}

function g(): true {
    const obj = new Child();
    obj.value = true;
    return obj.value
}

console.log(f()) // undefined
console.log(g()) // undefined

I think that most would expect Child to inherit Parent’s getter and behave the same as the following code: Playground Link

class Parent {
    protected _value = false;
    public get value() { return this._value; }
}

class Child extends Parent {
    public get value() { return this._value; }
    public set value(value: boolean) { this._value = value; }
}

function f(): boolean {
    const obj = new Child();
    return obj.value;
}

function g(): true {
    const obj = new Child();
    obj.value = true;
    return obj.value
}

console.log(f()) // false
console.log(g()) // true

0reactions
Jack-Workscommented, Nov 29, 2022

just being hit by this issue. The typeScript should error if the parent class has getter/setter but the child only have setter, because in most cases, people’s intention is to “inherit” the getter from the parent class.

class Parent {
    get a() {
        return 1
    }
    set a(val: number) {}
}
class Child extends Parent {
    set a(val: number) {
        super.a = val
    }
}
const child = new Child()
child.a = 2
console.log('--- after write --')
console.log(child.a)
//          ts: child.a is number
//          js: child.a is undefined

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to call a property of the base class if this ... - Stack Overflow
In B, accessing the property getter of the parent class A: ... Although I'm not sure if python supports calling the base class...
Read more >
Class inheritance - The Modern JavaScript Tutorial
Class inheritance is a way for one class to extend another class. So we can create new functionality on top of the existing....
Read more >
Kotlin Inheritance, Method Overriding, and Property Overriding
The class that inherits the features of another class is called the Child class or Derived class or Sub class, and the class...
Read more >
Getters and Setters: Manage Attributes in Python
They're not inherited independently but as a whole. Therefore, when you override the getter method of a property inherited from a parent class, ......
Read more >
Classes - Object-Oriented Programming in Python
When we create an object of that data type, we call it an instance of a class. ... for these allowed attributes when...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found