not narrow static property without type annotation in constructor. (#39252)

* fix #39226

* fix lint.
This commit is contained in:
ShuiRuTian 2020-06-27 02:08:35 +08:00 committed by GitHub
parent eb2f4e2337
commit 4601a786aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 71 additions and 1 deletions

View File

@ -7825,7 +7825,7 @@ namespace ts {
return addOptionality(type, isOptional);
}
if (isPropertyDeclaration(declaration) && (noImplicitAny || isInJSFile(declaration))) {
if (isPropertyDeclaration(declaration) && !hasStaticModifier(declaration) && (noImplicitAny || isInJSFile(declaration))) {
// We have a property declaration with no type annotation or initializer, in noImplicitAny mode or a .js file.
// Use control flow analysis of this.xxx assignments in the constructor to determine the type of the property.
const constructor = findConstructorDeclaration(declaration.parent);

View File

@ -0,0 +1,15 @@
tests/cases/compiler/staticVisibility2.ts(2,12): error TS7008: Member 'sideLength' implicitly has an 'any' type.
tests/cases/compiler/staticVisibility2.ts(4,14): error TS2576: Property 'sideLength' is a static member of type 'Square'
==== tests/cases/compiler/staticVisibility2.ts (2 errors) ====
class Square {
static sideLength;
~~~~~~~~~~
!!! error TS7008: Member 'sideLength' implicitly has an 'any' type.
constructor(sideLength: number) {
this.sideLength = sideLength;
~~~~~~~~~~
!!! error TS2576: Property 'sideLength' is a static member of type 'Square'
}
}

View File

@ -0,0 +1,15 @@
//// [staticVisibility2.ts]
class Square {
static sideLength;
constructor(sideLength: number) {
this.sideLength = sideLength;
}
}
//// [staticVisibility2.js]
var Square = /** @class */ (function () {
function Square(sideLength) {
this.sideLength = sideLength;
}
return Square;
}());

View File

@ -0,0 +1,15 @@
=== tests/cases/compiler/staticVisibility2.ts ===
class Square {
>Square : Symbol(Square, Decl(staticVisibility2.ts, 0, 0))
static sideLength;
>sideLength : Symbol(Square.sideLength, Decl(staticVisibility2.ts, 0, 14))
constructor(sideLength: number) {
>sideLength : Symbol(sideLength, Decl(staticVisibility2.ts, 2, 16))
this.sideLength = sideLength;
>this : Symbol(Square, Decl(staticVisibility2.ts, 0, 0))
>sideLength : Symbol(sideLength, Decl(staticVisibility2.ts, 2, 16))
}
}

View File

@ -0,0 +1,18 @@
=== tests/cases/compiler/staticVisibility2.ts ===
class Square {
>Square : Square
static sideLength;
>sideLength : any
constructor(sideLength: number) {
>sideLength : number
this.sideLength = sideLength;
>this.sideLength = sideLength : number
>this.sideLength : any
>this : this
>sideLength : any
>sideLength : number
}
}

View File

@ -0,0 +1,7 @@
// @noImplicitAny: true
class Square {
static sideLength;
constructor(sideLength: number) {
this.sideLength = sideLength;
}
}