Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion tsc/internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11989,12 +11989,24 @@ func (c *Checker) checkPropertyNotUsedBeforeDeclaration(prop *ast.Symbol, node *
}
var diagnostic *ast.Diagnostic
declarationName := right.Text()
// A `declare` instance property redeclaration emits nothing, so unlike a
// redeclaration without an initializer it cannot overwrite the base class
// field with undefined (see #43194). The base field is initialized in
// super(), so the ancestor exemption still applies for such a
// redeclaration. The exemption is limited to instance property
// declarations: an ambient global variable has no parent class, so
// checking it against an ancestor class would panic, and a static
// declaration must not be matched against the base class's instance
// properties.
isAmbientInstanceProperty := ast.IsPropertyDeclaration(valueDeclaration) &&
c.getCombinedModifierFlagsCached(valueDeclaration)&ast.ModifierFlagsAmbient != 0 &&
c.getCombinedModifierFlagsCached(valueDeclaration)&ast.ModifierFlagsStatic == 0
if c.isInPropertyInitializerOrClassStaticBlock(node, false /*ignoreArrowFunctions*/) &&
!c.isOptionalPropertyDeclaration(valueDeclaration) &&
!(ast.IsAccessExpression(node) && ast.IsAccessExpression(node.Expression())) &&
!c.isBlockScopedNameDeclaredBeforeUse(valueDeclaration, right) &&
!(ast.IsMethodDeclaration(valueDeclaration) && c.getCombinedModifierFlagsCached(valueDeclaration)&ast.ModifierFlagsStatic != 0) &&
(c.compilerOptions.GetUseDefineForClassFields() || !c.isPropertyDeclaredInAncestorClass(prop)) {
(c.compilerOptions.GetUseDefineForClassFields() && !isAmbientInstanceProperty || !c.isPropertyDeclaredInAncestorClass(prop)) {
diagnostic = c.error(right, diagnostics.Property_0_is_used_before_its_initialization, declarationName)
} else if ast.IsClassDeclaration(valueDeclaration) && !ast.IsTypeReferenceNode(node.Parent) && valueDeclaration.Flags&ast.NodeFlagsAmbient == 0 && !c.isBlockScopedNameDeclaredBeforeUse(valueDeclaration, right) {
diagnostic = c.error(right, diagnostics.Class_0_used_before_its_declaration, declarationName)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
declarePropertyRedeclaration.ts(16,14): error TS2729: Property 'b' is used before its initialization.
declarePropertyRedeclaration.ts(25,14): error TS2729: Property 'b' is used before its initialization.
declarePropertyRedeclaration.ts(33,27): error TS2729: Property 'v' is used before its initialization.
declarePropertyRedeclaration.ts(50,21): error TS2729: Property 'b' is used before its initialization.


==== declarePropertyRedeclaration.ts (4 errors) ====
class Base {
b: number = 1;
}

class Plain extends Base {
d = this.b;
}

class Narrowed extends Base {
declare b: 1 | 2;
d = this.b;
}

class Redeclared extends Base {
b: number;
d = this.b;
~
!!! error TS2729: Property 'b' is used before its initialization.
!!! related TS2728 declarePropertyRedeclaration.ts:15:5: 'b' is declared here.

constructor() {
super();
this.b = 2;
}
}

class Own {
d = this.b;
~
!!! error TS2729: Property 'b' is used before its initialization.
!!! related TS2728 declarePropertyRedeclaration.ts:26:5: 'b' is declared here.
b = 1;
}

// An ambient global has no parent class, so reading it in a static field
// initializer must not crash the compiler. The access is still reported,
// because the static initializer runs at class definition time.
class AmbientGlobalStatic {
static x = globalThis.v;
~
!!! error TS2729: Property 'v' is used before its initialization.
!!! related TS2728 declarePropertyRedeclaration.ts:36:13: 'v' is declared here.
}

declare var v: number;

// The same access from an instance field initializer is fine, because the
// initializer runs at construction time, after the module has evaluated.
class AmbientGlobalInstance {
x = globalThis.w;
}

declare var w: number;

// A static declaration must not be exempted: the ancestor check only looks
// at the base class's instance properties, and there is no inherited static
// b here, so Derived.d is undefined at runtime.
class StaticDeclared extends Base {
static d = this.b;
~
!!! error TS2729: Property 'b' is used before its initialization.
!!! related TS2728 declarePropertyRedeclaration.ts:51:20: 'b' is declared here.
declare static b: number;
}

// A static field reading an inherited static property is fine.
class StaticBase {
static b = 1;
}

class StaticDerived extends StaticBase {
static d = this.b;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//// [tests/cases/conformance/classes/propertyMemberDeclarations/declarePropertyRedeclaration.ts] ////

//// [declarePropertyRedeclaration.ts]
class Base {
b: number = 1;
}

class Plain extends Base {
d = this.b;
}

class Narrowed extends Base {
declare b: 1 | 2;
d = this.b;
}

class Redeclared extends Base {
b: number;
d = this.b;

constructor() {
super();
this.b = 2;
}
}

class Own {
d = this.b;
b = 1;
}

// An ambient global has no parent class, so reading it in a static field
// initializer must not crash the compiler. The access is still reported,
// because the static initializer runs at class definition time.
class AmbientGlobalStatic {
static x = globalThis.v;
}

declare var v: number;

// The same access from an instance field initializer is fine, because the
// initializer runs at construction time, after the module has evaluated.
class AmbientGlobalInstance {
x = globalThis.w;
}

declare var w: number;

// A static declaration must not be exempted: the ancestor check only looks
// at the base class's instance properties, and there is no inherited static
// b here, so Derived.d is undefined at runtime.
class StaticDeclared extends Base {
static d = this.b;
declare static b: number;
}

// A static field reading an inherited static property is fine.
class StaticBase {
static b = 1;
}

class StaticDerived extends StaticBase {
static d = this.b;
}


//// [declarePropertyRedeclaration.js]
"use strict";
class Base {
b = 1;
}
class Plain extends Base {
d = this.b;
}
class Narrowed extends Base {
d = this.b;
}
class Redeclared extends Base {
b;
d = this.b;
constructor() {
super();
this.b = 2;
}
}
class Own {
d = this.b;
b = 1;
}
// An ambient global has no parent class, so reading it in a static field
// initializer must not crash the compiler. The access is still reported,
// because the static initializer runs at class definition time.
class AmbientGlobalStatic {
static x = globalThis.v;
}
// The same access from an instance field initializer is fine, because the
// initializer runs at construction time, after the module has evaluated.
class AmbientGlobalInstance {
x = globalThis.w;
}
// A static declaration must not be exempted: the ancestor check only looks
// at the base class's instance properties, and there is no inherited static
// b here, so Derived.d is undefined at runtime.
class StaticDeclared extends Base {
static d = this.b;
}
// A static field reading an inherited static property is fine.
class StaticBase {
static b = 1;
}
class StaticDerived extends StaticBase {
static d = this.b;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// @noTypesAndSymbols: true
// @strictNullChecks: true
// @target: esnext
// @useDefineForClassFields: true

class Base {
b: number = 1;
}

class Plain extends Base {
d = this.b;
}

class Narrowed extends Base {
declare b: 1 | 2;
d = this.b;
}

class Redeclared extends Base {
b: number;
d = this.b;

constructor() {
super();
this.b = 2;
}
}

class Own {
d = this.b;
b = 1;
}

// An ambient global has no parent class, so reading it in a static field
// initializer must not crash the compiler. The access is still reported,
// because the static initializer runs at class definition time.
class AmbientGlobalStatic {
static x = globalThis.v;
}

declare var v: number;

// The same access from an instance field initializer is fine, because the
// initializer runs at construction time, after the module has evaluated.
class AmbientGlobalInstance {
x = globalThis.w;
}

declare var w: number;

// A static declaration must not be exempted: the ancestor check only looks
// at the base class's instance properties, and there is no inherited static
// b here, so Derived.d is undefined at runtime.
class StaticDeclared extends Base {
static d = this.b;
declare static b: number;
}

// A static field reading an inherited static property is fine.
class StaticBase {
static b = 1;
}

class StaticDerived extends StaticBase {
static d = this.b;
}