Skip to content

Commit 3e4578c

Browse files
dragomirtitianRyanCavanaugh
authored andcommitted
Fixed unreported strict property initialization violations. (#35891)
1 parent 071819b commit 3e4578c

File tree

9 files changed

+425
-126
lines changed

9 files changed

+425
-126
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32413,7 +32413,7 @@ namespace ts {
3241332413
}
3241432414
if (isInstancePropertyWithoutInitializer(member)) {
3241532415
const propName = (<PropertyDeclaration>member).name;
32416-
if (isIdentifier(propName)) {
32416+
if (isIdentifier(propName) || isPrivateIdentifier(propName)) {
3241732417
const type = getTypeOfSymbol(getSymbolOfNode(member));
3241832418
if (!(type.flags & TypeFlags.AnyOrUnknown || getFalsyFlags(type) & TypeFlags.Undefined)) {
3241932419
if (!constructor || !isPropertyInitializedInConstructor(propName, type, constructor)) {
@@ -32432,7 +32432,7 @@ namespace ts {
3243232432
!(<PropertyDeclaration>node).initializer;
3243332433
}
3243432434

32435-
function isPropertyInitializedInConstructor(propName: Identifier, propType: Type, constructor: ConstructorDeclaration) {
32435+
function isPropertyInitializedInConstructor(propName: Identifier | PrivateIdentifier, propType: Type, constructor: ConstructorDeclaration) {
3243632436
const reference = createPropertyAccess(createThis(), propName);
3243732437
reference.expression.parent = reference;
3243832438
reference.parent = constructor;

tests/baselines/reference/strictPropertyInitialization.errors.txt

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(4,5): error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor.
22
tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(6,5): error TS2564: Property 'c' has no initializer and is not definitely assigned in the constructor.
3-
tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(48,5): error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor.
4-
tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(71,5): error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor.
5-
tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(93,22): error TS2565: Property 'a' is used before being assigned.
6-
tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(94,23): error TS2565: Property 'b' is used before being assigned.
3+
tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(8,5): error TS2564: Property '#f' has no initializer and is not definitely assigned in the constructor.
4+
tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(10,5): error TS2564: Property '#h' has no initializer and is not definitely assigned in the constructor.
5+
tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(62,5): error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor.
6+
tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(63,5): error TS2564: Property '#b' has no initializer and is not definitely assigned in the constructor.
7+
tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(90,5): error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor.
8+
tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(113,22): error TS2565: Property 'a' is used before being assigned.
9+
tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(114,23): error TS2565: Property 'b' is used before being assigned.
10+
tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts(115,23): error TS2565: Property '#d' is used before being assigned.
711

812

9-
==== tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts (6 errors) ====
13+
==== tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitialization.ts (10 errors) ====
1014
// Properties with non-undefined types require initialization
1115

1216
class C1 {
@@ -18,6 +22,14 @@ tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitial
1822
~
1923
!!! error TS2564: Property 'c' has no initializer and is not definitely assigned in the constructor.
2024
d?: number;
25+
#f: number; //Error
26+
~~
27+
!!! error TS2564: Property '#f' has no initializer and is not definitely assigned in the constructor.
28+
#g: number | undefined;
29+
#h: number | null; //Error
30+
~~
31+
!!! error TS2564: Property '#h' has no initializer and is not definitely assigned in the constructor.
32+
#i?: number;
2133
}
2234

2335
// No strict initialization checks in ambient contexts
@@ -27,6 +39,11 @@ tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitial
2739
b: number | undefined;
2840
c: number | null;
2941
d?: number;
42+
43+
#f: number;
44+
#g: number | undefined;
45+
#h: number | null;
46+
#i?: number;
3047
}
3148

3249
// No strict initialization checks for static members
@@ -44,14 +61,19 @@ tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitial
4461
a = 0;
4562
b: number = 0;
4663
c: string = "abc";
64+
#d = 0
65+
#e: number = 0
66+
#f: string= "abc"
4767
}
4868

4969
// Assignment in constructor satisfies strict initialization check
5070

5171
class C5 {
5272
a: number;
73+
#b: number;
5374
constructor() {
5475
this.a = 0;
76+
this.#b = 0;
5577
}
5678
}
5779

@@ -61,22 +83,29 @@ tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitial
6183
a: number; // Error
6284
~
6385
!!! error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor.
86+
#b: number
87+
~~
88+
!!! error TS2564: Property '#b' has no initializer and is not definitely assigned in the constructor.
6489
constructor(cond: boolean) {
6590
if (cond) {
6691
return;
6792
}
6893
this.a = 0;
94+
this.#b = 0;
6995
}
7096
}
7197

7298
class C7 {
7399
a: number;
100+
#b: number;
74101
constructor(cond: boolean) {
75102
if (cond) {
76103
this.a = 1;
104+
this.#b = 1;
77105
return;
78106
}
79107
this.a = 0;
108+
this.#b = 1;
80109
}
81110
}
82111

@@ -106,14 +135,19 @@ tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitial
106135
a: number;
107136
b: number;
108137
c?: number;
138+
#d: number;
109139
constructor() {
110140
let x = this.a; // Error
111141
~
112142
!!! error TS2565: Property 'a' is used before being assigned.
113143
this.a = this.b; // Error
114144
~
115145
!!! error TS2565: Property 'b' is used before being assigned.
146+
this.b = this.#d //Error
147+
~~
148+
!!! error TS2565: Property '#d' is used before being assigned.
116149
this.b = x;
150+
this.#d = x;
117151
let y = this.c;
118152
}
119153
}
@@ -124,8 +158,10 @@ tests/cases/conformance/classes/propertyMemberDeclarations/strictPropertyInitial
124158

125159
class C11 {
126160
a: number;
161+
#b: number;
127162
constructor() {
128163
this.a = someValue();
164+
this.#b = someValue();
129165
}
130166
}
131167

0 commit comments

Comments
 (0)