Skip to content

Commit 7c09657

Browse files
authored
Merge pull request #29756 from Microsoft/fixStrictPropertyInitialization
Revert change to strict property initialization checks
2 parents 1ec8a71 + 16cf5d1 commit 7c09657

6 files changed

Lines changed: 87 additions & 1 deletion

File tree

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27249,7 +27249,7 @@ namespace ts {
2724927249
reference.expression.parent = reference;
2725027250
reference.parent = constructor;
2725127251
reference.flowNode = constructor.returnFlowNode;
27252-
const flowType = getFlowTypeOfReference(reference, getOptionalType(propType));
27252+
const flowType = getFlowTypeOfReference(reference, propType, getOptionalType(propType));
2725327253
return !(getFalsyFlags(flowType) & TypeFlags.Undefined);
2725427254
}
2725527255

tests/baselines/reference/strictPropertyInitialization.errors.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,15 @@ tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitial
117117
let y = this.c;
118118
}
119119
}
120+
121+
// Property is considered initialized by type any even though value could be undefined
122+
123+
declare function someValue(): any;
124+
125+
class C11 {
126+
a: number;
127+
constructor() {
128+
this.a = someValue();
129+
}
130+
}
120131

tests/baselines/reference/strictPropertyInitialization.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ class C10 {
9797
let y = this.c;
9898
}
9999
}
100+
101+
// Property is considered initialized by type any even though value could be undefined
102+
103+
declare function someValue(): any;
104+
105+
class C11 {
106+
a: number;
107+
constructor() {
108+
this.a = someValue();
109+
}
110+
}
100111

101112

102113
//// [strictPropertyInitialization.js]
@@ -172,6 +183,12 @@ var C10 = /** @class */ (function () {
172183
}
173184
return C10;
174185
}());
186+
var C11 = /** @class */ (function () {
187+
function C11() {
188+
this.a = someValue();
189+
}
190+
return C11;
191+
}());
175192

176193

177194
//// [strictPropertyInitialization.d.ts]
@@ -227,3 +244,8 @@ declare class C10 {
227244
c?: number;
228245
constructor();
229246
}
247+
declare function someValue(): any;
248+
declare class C11 {
249+
a: number;
250+
constructor();
251+
}

tests/baselines/reference/strictPropertyInitialization.symbols

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,23 @@ class C10 {
210210
}
211211
}
212212

213+
// Property is considered initialized by type any even though value could be undefined
214+
215+
declare function someValue(): any;
216+
>someValue : Symbol(someValue, Decl(strictPropertyInitialization.ts, 97, 1))
217+
218+
class C11 {
219+
>C11 : Symbol(C11, Decl(strictPropertyInitialization.ts, 101, 34))
220+
221+
a: number;
222+
>a : Symbol(C11.a, Decl(strictPropertyInitialization.ts, 103, 11))
223+
224+
constructor() {
225+
this.a = someValue();
226+
>this.a : Symbol(C11.a, Decl(strictPropertyInitialization.ts, 103, 11))
227+
>this : Symbol(C11, Decl(strictPropertyInitialization.ts, 101, 34))
228+
>a : Symbol(C11.a, Decl(strictPropertyInitialization.ts, 103, 11))
229+
>someValue : Symbol(someValue, Decl(strictPropertyInitialization.ts, 97, 1))
230+
}
231+
}
232+

tests/baselines/reference/strictPropertyInitialization.types

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,25 @@ class C10 {
227227
}
228228
}
229229

230+
// Property is considered initialized by type any even though value could be undefined
231+
232+
declare function someValue(): any;
233+
>someValue : () => any
234+
235+
class C11 {
236+
>C11 : C11
237+
238+
a: number;
239+
>a : number
240+
241+
constructor() {
242+
this.a = someValue();
243+
>this.a = someValue() : any
244+
>this.a : number
245+
>this : this
246+
>a : number
247+
>someValue() : any
248+
>someValue : () => any
249+
}
250+
}
251+

tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,14 @@ class C10 {
9999
let y = this.c;
100100
}
101101
}
102+
103+
// Property is considered initialized by type any even though value could be undefined
104+
105+
declare function someValue(): any;
106+
107+
class C11 {
108+
a: number;
109+
constructor() {
110+
this.a = someValue();
111+
}
112+
}

0 commit comments

Comments
 (0)