diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index aa49eeb8a2c..23459491518 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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); } 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 + } +} + 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(); + } +}