Skip to content

Why strictNullChecks option does not warn a non-nullable class property which is not initialized? #9761

Closed
@tetsuharuohzeki

Description

@tetsuharuohzeki

Is this a bug or by design?

Environment

  • TypeScript
    • 2.1.0-dev.20160715
    • 2.0.0

Steps to reproduce

compile the following code with tsc --strictNullChecks test.ts:

// test.ts

class A {
  a: Array<number>;
  constructor() {}
}

var a = new A();
// this would be the error which is like "Cannot read property 'length' of undefined"
// because `a.a` is not uninitialized in A's constructor.
a.a.length; 

Expected behavior

  • I'd like to this should be a compile error by following reasons:
    • A.a is not "nullable" (null or undefined) nor optional class property a?
    • But A.a is not initialized in A's constructor.

Actual behavior

  • TypeScript compiler successes the compilation without any errors.
    • I think this might be a pitfall.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixedDuplicateAn 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