Skip to content

Cannot set property 'xyz' of undefined #9247

Closed
@benjaminmillhouse

Description

@benjaminmillhouse

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions