diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index 694fc155b8e16..b8dba03f0033e 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -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) diff --git a/tsc/testdata/baselines/reference/conformance/declarePropertyRedeclaration.errors.txt b/tsc/testdata/baselines/reference/conformance/declarePropertyRedeclaration.errors.txt new file mode 100644 index 0000000000000..8914884a38e70 --- /dev/null +++ b/tsc/testdata/baselines/reference/conformance/declarePropertyRedeclaration.errors.txt @@ -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; + } + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/conformance/declarePropertyRedeclaration.js b/tsc/testdata/baselines/reference/conformance/declarePropertyRedeclaration.js new file mode 100644 index 0000000000000..762b126c525f4 --- /dev/null +++ b/tsc/testdata/baselines/reference/conformance/declarePropertyRedeclaration.js @@ -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; +} diff --git a/tsc/testdata/tests/cases/conformance/classes/propertyMemberDeclarations/declarePropertyRedeclaration.ts b/tsc/testdata/tests/cases/conformance/classes/propertyMemberDeclarations/declarePropertyRedeclaration.ts new file mode 100644 index 0000000000000..eff307c3ba26d --- /dev/null +++ b/tsc/testdata/tests/cases/conformance/classes/propertyMemberDeclarations/declarePropertyRedeclaration.ts @@ -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; +}