Merge pull request #29756 from Microsoft/fixStrictPropertyInitialization

Revert change to strict property initialization checks
This commit is contained in:
Anders Hejlsberg 2019-02-05 15:10:30 -08:00 committed by GitHub
commit 7c096576bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 87 additions and 1 deletions

View File

@ -27249,7 +27249,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);
}

View File

@ -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();
}
}

View File

@ -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();
}

View File

@ -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))
}
}

View File

@ -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
}
}

View File

@ -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();
}
}