Skip to content

Type flow analysis in constructor not working properly with strictPropertyInitialization #28100

@paul-go

Description

@paul-go

TypeScript Version: 3.2.0-dev.201xxxxx

Search Terms:
strictNullChecks
constructor

Code

(This may actually be two separate issues.)

// A *self-contained* demonstration of the problem follows...
// Test this by running `tsc` on the command-line, rather than through another build tool such as Gulp, Webpack, etc.

class X
{
	constructor()
	{
		// Commenting and uncommenting this supposedly no-op line
		// causes a definite assignment error to be reported on the private x field.
		if (this instanceof X) { }
		false && (this.x = null!);
		
		// Possibly related, but statically verifyable truthy expressions
		// don't cause definite assignment verification to be fulfilled.
		
		// Causes the definite assignment condition to be fulfilled.
		true && (this.x = null!);
		
		// *Should* cause the definite assigment condition to be fulfilled,
		// but does not appear to do so.
		"I'm true" && (this.x = null!);
		1 && (this.x = null!);
	}
	
	private x: X;
}

Associated tsconfig file:

{
	"compilerOptions": {
		"module": "commonjs",
		"target": "es2017",
		"lib": [
			"es7",
			"es2017",
			"es2017.object"
		],
		"inlineSourceMap": true,
		"inlineSources": true,
		"noEmit": true,
		"noImplicitThis": true,
		"strict": true,
		"strictNullChecks": true,
		"rootDir": "./",
		"baseUrl": "./",
		"stripInternal": true
	}
}

Expected behavior:
Uncommenting the if (this instanceof X) { } line should have no effects on the rest of the method. I would also expect expressions whose computed values are statically verifiable to contribute to type flow analysis, rather than just the "true" / "false" literals. Of course there is a very long tail to how far this could be taken, but as it stands, my original intent of using truthy literals as compilation constants (that are replaced in the build process) can't be realized with strictNullChecks:

"__DEBUG__" && this.someFieldOnlyVisibleDuringTests = ...

(Unless of course I just exploit this bug behavior)

Actual behavior:
Covered above.

Playground Link:
(I'm fairly sure strictNullChecks aren't an option in the playground)

Related Issues:

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions