Skip to content

strictNullChecks does not ensure class properties are initialized in constructor #10827

@Swatinem

Description

@Swatinem

TypeScript Version: 2.1.0-dev.20160909

Code

class A {
    prop: string;
}

const a = new A();
a.prop.length; // TypeError: Cannot read property 'length' of undefined

Expected behavior:

TS should throw a compile time error to ensure that non-nullable class members are initialized inside the constructor and can not lead to runtime errors.

Actual behavior:

No compile time error, rather an exception at runtime

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions