Skip to content

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

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
tetsuharuohzeki opened this issue Jul 15, 2016 · 2 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed Duplicate An existing issue was already created

Comments

@tetsuharuohzeki
Copy link
Contributor

tetsuharuohzeki commented Jul 15, 2016

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.
@sandersn
Copy link
Member

This is currently by design, but it's a design limitation. We would like to allow a to be initialised in the constructor or somewhere else, not just directly on the property. But there's no simple way to know which code is used for initialisation (where a would be allowed to be undefined) and which should require a to be defined.

I don't know if there's an existing bug about this, but we've spent hours arguing about it in design meetings. I'll see if I can find something.

@sandersn sandersn added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Jul 15, 2016
@sandersn
Copy link
Member

Here it is: #8476

@sandersn sandersn added the Duplicate An existing issue was already created label Jul 15, 2016
@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
Design Limitation Constraints of the existing architecture prevent this from being fixed Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants