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

missing getter is undetected TS error, runtime error #37689

Closed
bunnybones1 opened this issue Mar 30, 2020 · 3 comments
Closed

missing getter is undetected TS error, runtime error #37689

bunnybones1 opened this issue Mar 30, 2020 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@bunnybones1
Copy link

bunnybones1 commented Mar 30, 2020

TypeScript Version: 3.8.3

Search Terms:
missing getter setter

Code

class A {
  label:string = 'test'
}

class B {
  private _a:A
  set a(val:A) { this._a = val }
  constructor() { this._a = new A() }
}
const b = new B()
b.a.label = 'hello' //b.a will always be undefined due to missing getter, but still somehow gets b.a's type from the setter, and will cause no TS errors

Expected behavior:

Error: no 'a' property on class 'B'

OR

Error: class 'B' is missing a getter for property 'a'

(P.S. maybe b.a should not resolve to type 'A' just because that is the setter type)

Actual behavior:

Compiles fine. Variable has value undefined at runtime.

Playground Link:
Playground Link

Related Issues:
#30852

@arilotter
Copy link

+1 A getter without a setter is well-defined in TS (readonly) but a setter without a getter should either use some writeonly modifier ( #21759 ) or fail to compile entirely, since TS's type system won't complain and you'll get runtime errors.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 31, 2020
@bunnybones1
Copy link
Author

Hey @RyanCavanaugh
I'm aware of #30852 but it's closed and unresolved, so I wasn't sure how to proceed.

@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