From 4601a786aa76202e7ecdf6e5a0b4a10ebc6cd35a Mon Sep 17 00:00:00 2001 From: ShuiRuTian <158983297@qq.com> Date: Sat, 27 Jun 2020 02:08:35 +0800 Subject: [PATCH] not narrow static property without type annotation in constructor. (#39252) * fix #39226 * fix lint. --- src/compiler/checker.ts | 2 +- .../reference/staticVisibility2.errors.txt | 15 +++++++++++++++ tests/baselines/reference/staticVisibility2.js | 15 +++++++++++++++ .../reference/staticVisibility2.symbols | 15 +++++++++++++++ .../reference/staticVisibility2.types | 18 ++++++++++++++++++ tests/cases/compiler/staticVisibility2.ts | 7 +++++++ 6 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/staticVisibility2.errors.txt create mode 100644 tests/baselines/reference/staticVisibility2.js create mode 100644 tests/baselines/reference/staticVisibility2.symbols create mode 100644 tests/baselines/reference/staticVisibility2.types create mode 100644 tests/cases/compiler/staticVisibility2.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b3d1183b6f3..aa0091f9952 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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); diff --git a/tests/baselines/reference/staticVisibility2.errors.txt b/tests/baselines/reference/staticVisibility2.errors.txt new file mode 100644 index 00000000000..b217a0a3fdd --- /dev/null +++ b/tests/baselines/reference/staticVisibility2.errors.txt @@ -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' + } + } \ No newline at end of file diff --git a/tests/baselines/reference/staticVisibility2.js b/tests/baselines/reference/staticVisibility2.js new file mode 100644 index 00000000000..70ffb3a9f18 --- /dev/null +++ b/tests/baselines/reference/staticVisibility2.js @@ -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; +}()); diff --git a/tests/baselines/reference/staticVisibility2.symbols b/tests/baselines/reference/staticVisibility2.symbols new file mode 100644 index 00000000000..021d5833c4a --- /dev/null +++ b/tests/baselines/reference/staticVisibility2.symbols @@ -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)) + } +} diff --git a/tests/baselines/reference/staticVisibility2.types b/tests/baselines/reference/staticVisibility2.types new file mode 100644 index 00000000000..6243cd21a2f --- /dev/null +++ b/tests/baselines/reference/staticVisibility2.types @@ -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 + } +} diff --git a/tests/cases/compiler/staticVisibility2.ts b/tests/cases/compiler/staticVisibility2.ts new file mode 100644 index 00000000000..e27b63c38f9 --- /dev/null +++ b/tests/cases/compiler/staticVisibility2.ts @@ -0,0 +1,7 @@ +// @noImplicitAny: true +class Square { + static sideLength; + constructor(sideLength: number) { + this.sideLength = sideLength; + } +} \ No newline at end of file