Skip to content
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

Inheritance of getter #25927

Closed
wehrstedt opened this issue Jul 25, 2018 · 2 comments
Closed

Inheritance of getter #25927

wehrstedt opened this issue Jul 25, 2018 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@wehrstedt
Copy link

TypeScript Version: 2.7.1

Search Terms:
getter inheritance
getter derived
accessor inheritance

Code

class A {
	protected _oId: string;

	constructor(){
		this._oId = "";
	}

	public get oId(): string {
		return this._oId;
	}
}

class B extends A {
	constructor() {
		super();
	}

	public set oId(val: string) {
		this._oId = val;
	}
}

Expected behavior:

const b = new B();
b.oId = "123";
console.log(b.oId); // 123

If I understand #13432 right, it's a correct behaviour. But then it's not correct that getters and setters are inherited in general. I have to overwrite the getter function in my derived class to make the code work as expected. But then the benefit of inheritance is lost in that case, more concretely the inheritence makes no sense at this point because the logic of the parent class is lost. The main problem is that no exception occurres and it's hard to find the cause of this error.

Actual behavior:

const b = new B();
b.oId = "123";
console.log(b.oId); // undefined

Playground Link:
Click

Related Issues:
#13432

@ghost
Copy link

ghost commented Jul 25, 2018

Duplicate of #11596

@ghost ghost marked this as a duplicate of #11596 Jul 25, 2018
@ghost ghost added the Duplicate An existing issue was already created label Jul 25, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

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

2 participants