Skip to content

Cannot set property 'xyz' of undefined #9247

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
benjaminmillhouse opened this issue Jun 18, 2016 · 3 comments
Closed

Cannot set property 'xyz' of undefined #9247

benjaminmillhouse opened this issue Jun 18, 2016 · 3 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@benjaminmillhouse
Copy link

It would seem like this is a (common?) bug that the compiler could catch for us.

Given this example class:

class Foo {
    bar: number;
}
let a: Foo;
a.bar = 1;

At runtime I will get an error 'Cannot set property 'bar' of undefined because I never actually initialized the object. (i.e. let a: Foo = new Foo(); or let a = new Foo();)
This seems to me like it could probably be a pretty common developer mistake given the ability to provide types (: Foo), especially since editors will give you full Intellisense on the object just by naming the type of it without initialization. I'm curious if it's something that the Compiler could catch? (variable a is undefined; declared, but never initialized)

@yortus
Copy link
Contributor

yortus commented Jun 18, 2016

If you use the --strictNullChecks compiler option, you'll get a compiler error stating Variable 'a' is used before being assigned. Is that what you are after?

@benjaminmillhouse
Copy link
Author

That probably would do it. Sorry, didn't see that in the tsconfig options. I'll give it a try.

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Jun 18, 2016
@yortus
Copy link
Contributor

yortus commented Jun 18, 2016

@Benmillhouse it's only in the nightly version, so might not be documented yet. See #7140.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants