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
2 changes: 1 addition & 1 deletion tsc/internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11990,7 +11990,7 @@ func (c *Checker) checkPropertyNotUsedBeforeDeclaration(prop *ast.Symbol, node *
!(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.isPropertyDeclaredInAncestorClass(prop) || c.compilerOptions.GetUseDefineForClassFields() && valueDeclaration.ModifierFlags()&ast.ModifierFlagsAmbient == 0) {
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,31 @@
useBeforeDeclaration_declareRedeclaration.ts(15,5): error TS2564: Property 'b' has no initializer and is not definitely assigned in the constructor.
useBeforeDeclaration_declareRedeclaration.ts(15,5): error TS2612: Property 'b' will overwrite the base property in 'Base'. If this is intentional, add an initializer. Otherwise, add a 'declare' modifier or remove the redundant declaration.
useBeforeDeclaration_declareRedeclaration.ts(16,14): error TS2729: Property 'b' is used before its initialization.


==== useBeforeDeclaration_declareRedeclaration.ts (3 errors) ====
class Base {
b: 1 = 1;
}

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

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

class Redeclared extends Base {
b: 1;
~
!!! error TS2564: Property 'b' has no initializer and is not definitely assigned in the constructor.
~
!!! error TS2612: Property 'b' will overwrite the base property in 'Base'. If this is intentional, add an initializer. Otherwise, add a 'declare' modifier or remove the redundant declaration.
d = this.b;
~
!!! error TS2729: Property 'b' is used before its initialization.
!!! related TS2728 useBeforeDeclaration_declareRedeclaration.ts:15:5: 'b' is declared here.
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//// [tests/cases/compiler/useBeforeDeclaration_declareRedeclaration.ts] ////

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

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

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

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


//// [useBeforeDeclaration_declareRedeclaration.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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//// [tests/cases/compiler/useBeforeDeclaration_declareRedeclaration.ts] ////

=== useBeforeDeclaration_declareRedeclaration.ts ===
class Base {
>Base : Symbol(Base, Decl(useBeforeDeclaration_declareRedeclaration.ts, 0, 0))

b: 1 = 1;
>b : Symbol(Base.b, Decl(useBeforeDeclaration_declareRedeclaration.ts, 0, 12))
}

class Plain extends Base {
>Plain : Symbol(Plain, Decl(useBeforeDeclaration_declareRedeclaration.ts, 2, 1))
>Base : Symbol(Base, Decl(useBeforeDeclaration_declareRedeclaration.ts, 0, 0))

d = this.b;
>d : Symbol(Plain.d, Decl(useBeforeDeclaration_declareRedeclaration.ts, 4, 26))
>this.b : Symbol(Base.b, Decl(useBeforeDeclaration_declareRedeclaration.ts, 0, 12))
>this : Symbol(Plain, Decl(useBeforeDeclaration_declareRedeclaration.ts, 2, 1))
>b : Symbol(Base.b, Decl(useBeforeDeclaration_declareRedeclaration.ts, 0, 12))
}

class Narrowed extends Base {
>Narrowed : Symbol(Narrowed, Decl(useBeforeDeclaration_declareRedeclaration.ts, 6, 1))
>Base : Symbol(Base, Decl(useBeforeDeclaration_declareRedeclaration.ts, 0, 0))

declare b: 1;
>b : Symbol(Narrowed.b, Decl(useBeforeDeclaration_declareRedeclaration.ts, 8, 29))

d = this.b;
>d : Symbol(Narrowed.d, Decl(useBeforeDeclaration_declareRedeclaration.ts, 9, 17))
>this.b : Symbol(Narrowed.b, Decl(useBeforeDeclaration_declareRedeclaration.ts, 8, 29))
>this : Symbol(Narrowed, Decl(useBeforeDeclaration_declareRedeclaration.ts, 6, 1))
>b : Symbol(Narrowed.b, Decl(useBeforeDeclaration_declareRedeclaration.ts, 8, 29))
}

class Redeclared extends Base {
>Redeclared : Symbol(Redeclared, Decl(useBeforeDeclaration_declareRedeclaration.ts, 11, 1))
>Base : Symbol(Base, Decl(useBeforeDeclaration_declareRedeclaration.ts, 0, 0))

b: 1;
>b : Symbol(Redeclared.b, Decl(useBeforeDeclaration_declareRedeclaration.ts, 13, 31))

d = this.b;
>d : Symbol(Redeclared.d, Decl(useBeforeDeclaration_declareRedeclaration.ts, 14, 9))
>this.b : Symbol(Redeclared.b, Decl(useBeforeDeclaration_declareRedeclaration.ts, 13, 31))
>this : Symbol(Redeclared, Decl(useBeforeDeclaration_declareRedeclaration.ts, 11, 1))
>b : Symbol(Redeclared.b, Decl(useBeforeDeclaration_declareRedeclaration.ts, 13, 31))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//// [tests/cases/compiler/useBeforeDeclaration_declareRedeclaration.ts] ////

=== useBeforeDeclaration_declareRedeclaration.ts ===
class Base {
>Base : Base

b: 1 = 1;
>b : 1
>1 : 1
}

class Plain extends Base {
>Plain : Plain
>Base : Base

d = this.b;
>d : 1
>this.b : 1
>this : this
>b : 1
}

class Narrowed extends Base {
>Narrowed : Narrowed
>Base : Base

declare b: 1;
>b : 1

d = this.b;
>d : 1
>this.b : 1
>this : this
>b : 1
}

class Redeclared extends Base {
>Redeclared : Redeclared
>Base : Base

b: 1;
>b : 1

d = this.b;
>d : 1
>this.b : 1
>this : this
>b : 1
}

2 changes: 1 addition & 1 deletion tsc/testdata/fixtures/compiler/checker.ts

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you update this test fixture

Original file line number Diff line number Diff line change
Expand Up @@ -35094,7 +35094,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
&& !(isAccessExpression(node) && isAccessExpression(node.expression))
&& !isBlockScopedNameDeclaredBeforeUse(valueDeclaration, right)
&& !(isMethodDeclaration(valueDeclaration) && getCombinedModifierFlagsCached(valueDeclaration) & ModifierFlags.Static)
&& (useDefineForClassFields || !isPropertyDeclaredInAncestorClass(prop))
&& (!isPropertyDeclaredInAncestorClass(prop) || useDefineForClassFields && !(getEffectiveModifierFlags(valueDeclaration) & ModifierFlags.Ambient))
) {
diagnosticMessage = error(right, Diagnostics.Property_0_is_used_before_its_initialization, declarationName);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// @target: es2022
// @strict: true
// @useDefineForClassFields: true

class Base {
b: 1 = 1;
}

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

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

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