Skip to content

Commit 4601a78

Browse files
authored
not narrow static property without type annotation in constructor. (#39252)
* fix #39226 * fix lint.
1 parent eb2f4e2 commit 4601a78

File tree

6 files changed

+71
-1
lines changed

6 files changed

+71
-1
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7825,7 +7825,7 @@ namespace ts {
78257825
return addOptionality(type, isOptional);
78267826
}
78277827

7828-
if (isPropertyDeclaration(declaration) && (noImplicitAny || isInJSFile(declaration))) {
7828+
if (isPropertyDeclaration(declaration) && !hasStaticModifier(declaration) && (noImplicitAny || isInJSFile(declaration))) {
78297829
// We have a property declaration with no type annotation or initializer, in noImplicitAny mode or a .js file.
78307830
// Use control flow analysis of this.xxx assignments in the constructor to determine the type of the property.
78317831
const constructor = findConstructorDeclaration(declaration.parent);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
tests/cases/compiler/staticVisibility2.ts(2,12): error TS7008: Member 'sideLength' implicitly has an 'any' type.
2+
tests/cases/compiler/staticVisibility2.ts(4,14): error TS2576: Property 'sideLength' is a static member of type 'Square'
3+
4+
5+
==== tests/cases/compiler/staticVisibility2.ts (2 errors) ====
6+
class Square {
7+
static sideLength;
8+
~~~~~~~~~~
9+
!!! error TS7008: Member 'sideLength' implicitly has an 'any' type.
10+
constructor(sideLength: number) {
11+
this.sideLength = sideLength;
12+
~~~~~~~~~~
13+
!!! error TS2576: Property 'sideLength' is a static member of type 'Square'
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [staticVisibility2.ts]
2+
class Square {
3+
static sideLength;
4+
constructor(sideLength: number) {
5+
this.sideLength = sideLength;
6+
}
7+
}
8+
9+
//// [staticVisibility2.js]
10+
var Square = /** @class */ (function () {
11+
function Square(sideLength) {
12+
this.sideLength = sideLength;
13+
}
14+
return Square;
15+
}());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/compiler/staticVisibility2.ts ===
2+
class Square {
3+
>Square : Symbol(Square, Decl(staticVisibility2.ts, 0, 0))
4+
5+
static sideLength;
6+
>sideLength : Symbol(Square.sideLength, Decl(staticVisibility2.ts, 0, 14))
7+
8+
constructor(sideLength: number) {
9+
>sideLength : Symbol(sideLength, Decl(staticVisibility2.ts, 2, 16))
10+
11+
this.sideLength = sideLength;
12+
>this : Symbol(Square, Decl(staticVisibility2.ts, 0, 0))
13+
>sideLength : Symbol(sideLength, Decl(staticVisibility2.ts, 2, 16))
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/compiler/staticVisibility2.ts ===
2+
class Square {
3+
>Square : Square
4+
5+
static sideLength;
6+
>sideLength : any
7+
8+
constructor(sideLength: number) {
9+
>sideLength : number
10+
11+
this.sideLength = sideLength;
12+
>this.sideLength = sideLength : number
13+
>this.sideLength : any
14+
>this : this
15+
>sideLength : any
16+
>sideLength : number
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @noImplicitAny: true
2+
class Square {
3+
static sideLength;
4+
constructor(sideLength: number) {
5+
this.sideLength = sideLength;
6+
}
7+
}

0 commit comments

Comments
 (0)