From 2256d761a20616434d3e20d2f7e0caa46ea7e1c3 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 5 Feb 2019 12:40:25 -0800 Subject: [PATCH 1/3] Revert change to strict property initialization checks --- src/compiler/checker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ce06e84b2fd..7d9d36b77b6 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -27225,7 +27225,7 @@ namespace ts { reference.expression.parent = reference; reference.parent = constructor; reference.flowNode = constructor.returnFlowNode; - const flowType = getFlowTypeOfReference(reference, getOptionalType(propType)); + const flowType = getFlowTypeOfReference(reference, propType, getOptionalType(propType)); return !(getFalsyFlags(flowType) & TypeFlags.Undefined); } From 2d44a402025d5f98db7ac83d7ad274015f5d8094 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 5 Feb 2019 13:08:18 -0800 Subject: [PATCH 2/3] Add regression test --- .../strictPropertyInitialization.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts b/tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts index 01177200429..fc03101922a 100644 --- a/tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts +++ b/tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts @@ -99,3 +99,14 @@ class C10 { let y = this.c; } } + +// Property is considered initialized by type any even though value could be undefined + +declare function someValue(): any; + +class C11 { + a: number; + constructor() { + this.a = someValue(); + } +} From 16cf5d11cc78101991f09e2e16c48e1d509ad1b9 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 5 Feb 2019 13:08:25 -0800 Subject: [PATCH 3/3] Accept new baselines --- .../strictPropertyInitialization.errors.txt | 11 ++++++++++ .../reference/strictPropertyInitialization.js | 22 +++++++++++++++++++ .../strictPropertyInitialization.symbols | 20 +++++++++++++++++ .../strictPropertyInitialization.types | 22 +++++++++++++++++++ 4 files changed, 75 insertions(+) diff --git a/tests/baselines/reference/strictPropertyInitialization.errors.txt b/tests/baselines/reference/strictPropertyInitialization.errors.txt index 4a3fe0eb3e5..17283f61162 100644 --- a/tests/baselines/reference/strictPropertyInitialization.errors.txt +++ b/tests/baselines/reference/strictPropertyInitialization.errors.txt @@ -117,4 +117,15 @@ tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitial let y = this.c; } } + + // Property is considered initialized by type any even though value could be undefined + + declare function someValue(): any; + + class C11 { + a: number; + constructor() { + this.a = someValue(); + } + } \ No newline at end of file diff --git a/tests/baselines/reference/strictPropertyInitialization.js b/tests/baselines/reference/strictPropertyInitialization.js index 6de5084e40c..3593fdb37f8 100644 --- a/tests/baselines/reference/strictPropertyInitialization.js +++ b/tests/baselines/reference/strictPropertyInitialization.js @@ -97,6 +97,17 @@ class C10 { let y = this.c; } } + +// Property is considered initialized by type any even though value could be undefined + +declare function someValue(): any; + +class C11 { + a: number; + constructor() { + this.a = someValue(); + } +} //// [strictPropertyInitialization.js] @@ -172,6 +183,12 @@ var C10 = /** @class */ (function () { } return C10; }()); +var C11 = /** @class */ (function () { + function C11() { + this.a = someValue(); + } + return C11; +}()); //// [strictPropertyInitialization.d.ts] @@ -227,3 +244,8 @@ declare class C10 { c?: number; constructor(); } +declare function someValue(): any; +declare class C11 { + a: number; + constructor(); +} diff --git a/tests/baselines/reference/strictPropertyInitialization.symbols b/tests/baselines/reference/strictPropertyInitialization.symbols index c271094a527..6421d0b6920 100644 --- a/tests/baselines/reference/strictPropertyInitialization.symbols +++ b/tests/baselines/reference/strictPropertyInitialization.symbols @@ -210,3 +210,23 @@ class C10 { } } +// Property is considered initialized by type any even though value could be undefined + +declare function someValue(): any; +>someValue : Symbol(someValue, Decl(strictPropertyInitialization.ts, 97, 1)) + +class C11 { +>C11 : Symbol(C11, Decl(strictPropertyInitialization.ts, 101, 34)) + + a: number; +>a : Symbol(C11.a, Decl(strictPropertyInitialization.ts, 103, 11)) + + constructor() { + this.a = someValue(); +>this.a : Symbol(C11.a, Decl(strictPropertyInitialization.ts, 103, 11)) +>this : Symbol(C11, Decl(strictPropertyInitialization.ts, 101, 34)) +>a : Symbol(C11.a, Decl(strictPropertyInitialization.ts, 103, 11)) +>someValue : Symbol(someValue, Decl(strictPropertyInitialization.ts, 97, 1)) + } +} + diff --git a/tests/baselines/reference/strictPropertyInitialization.types b/tests/baselines/reference/strictPropertyInitialization.types index 4c0ab3e8527..f1fe49e94e8 100644 --- a/tests/baselines/reference/strictPropertyInitialization.types +++ b/tests/baselines/reference/strictPropertyInitialization.types @@ -227,3 +227,25 @@ class C10 { } } +// Property is considered initialized by type any even though value could be undefined + +declare function someValue(): any; +>someValue : () => any + +class C11 { +>C11 : C11 + + a: number; +>a : number + + constructor() { + this.a = someValue(); +>this.a = someValue() : any +>this.a : number +>this : this +>a : number +>someValue() : any +>someValue : () => any + } +} +