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

Overriding property type leads to possible incorrect value assignment. #36504

Closed
bogdan opened this issue Jan 29, 2020 · 1 comment
Closed

Overriding property type leads to possible incorrect value assignment. #36504

bogdan opened this issue Jan 29, 2020 · 1 comment

Comments

@bogdan
Copy link

bogdan commented Jan 29, 2020

TypeScript Version: 3.5.3

Search Terms:

property inheritance type override

Code

class A {
  params: {} = {}

  resetParams() {
    this.params = {};
  }
}

class B extends A {
  params: {b: number} = {b: 1};
}

const b = new B()
b.params // => {b: 1};
b.resetParams();
b.params // => {};

Expected behavior:

A type of params is strictly checked for incorrect assignment.
I do not see any solution to this myself other than disallow to override property type.
We can search how such cases are handled in other statically typed language like Java.

Actual behavior:

Typescript allows incorrect value assignment without any error.

Playground Link:
https://www.typescriptlang.org/play/?ssl=14&ssc=1&pln=15&pc=1#code/MYGwhgzhAECC0G8CwAoa0AOYBOYC2EAXIgL7QC8pqq62AphHQC4AKO+EAFAJSI3rQmACwCWEAHRZcBCqQDc-EqiUpUoSDABC0OgA8mdAHYATGPGRpM7AsQQAjYoYCueO3WxlK94gEYSClBU1AHtDCCZoO1lDOgB3aE0eELDgkDpxEGCAc047SWsIbgC8+kZWAp4A4FCIVPTMnLypDiLUIA

Related Issues:

In this issue, there is an example where typescript tracks a possibility of property type being override and rejects to compile: #35466

@AnyhowStep
Copy link
Contributor

AnyhowStep commented Jan 29, 2020

#33234

#18770

@bogdan bogdan closed this as completed Jan 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants