Skip to content

Commit 6c175a0

Browse files
authored
Fix isPropertyDeclaredInAncestorClass function (#2376)
1 parent 2046634 commit 6c175a0

File tree

5 files changed

+86
-13
lines changed

5 files changed

+86
-13
lines changed

internal/checker/checker.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11368,21 +11368,13 @@ func (c *Checker) isOptionalPropertyDeclaration(node *ast.Node) bool {
1136811368
}
1136911369

1137011370
func (c *Checker) isPropertyDeclaredInAncestorClass(prop *ast.Symbol) bool {
11371-
if prop.Parent.Flags&ast.SymbolFlagsClass == 0 {
11372-
return false
11373-
}
11374-
classType := c.getDeclaredTypeOfSymbol(prop.Parent)
11375-
for {
11376-
baseTypes := c.getBaseTypes(classType)
11377-
if len(baseTypes) == 0 {
11378-
return false
11379-
}
11380-
classType = baseTypes[0]
11381-
superProperty := c.getPropertyOfType(classType, prop.Name)
11382-
if superProperty != nil && superProperty.ValueDeclaration != nil {
11383-
return true
11371+
if prop.Parent.Flags&ast.SymbolFlagsClass != 0 {
11372+
if baseTypes := c.getBaseTypes(c.getDeclaredTypeOfSymbol(prop.Parent)); len(baseTypes) != 0 {
11373+
superProperty := c.getPropertyOfType(baseTypes[0], prop.Name)
11374+
return superProperty != nil && superProperty.ValueDeclaration != nil
1138411375
}
1138511376
}
11377+
return false
1138611378
}
1138711379

1138811380
/**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
checkInheritedProperty.ts(7,14): error TS2729: Property 'b' is used before its initialization.
2+
3+
4+
==== checkInheritedProperty.ts (1 errors) ====
5+
class Base {
6+
}
7+
8+
declare const BaseFactory: new() => Base & { c: string }
9+
10+
class Derived extends BaseFactory {
11+
a = this.b
12+
~
13+
!!! error TS2729: Property 'b' is used before its initialization.
14+
!!! related TS2728 checkInheritedProperty.ts:8:5: 'b' is declared here.
15+
b = "abc"
16+
}
17+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//// [tests/cases/compiler/checkInheritedProperty.ts] ////
2+
3+
=== checkInheritedProperty.ts ===
4+
class Base {
5+
>Base : Symbol(Base, Decl(checkInheritedProperty.ts, 0, 0))
6+
}
7+
8+
declare const BaseFactory: new() => Base & { c: string }
9+
>BaseFactory : Symbol(BaseFactory, Decl(checkInheritedProperty.ts, 3, 13))
10+
>Base : Symbol(Base, Decl(checkInheritedProperty.ts, 0, 0))
11+
>c : Symbol(c, Decl(checkInheritedProperty.ts, 3, 44))
12+
13+
class Derived extends BaseFactory {
14+
>Derived : Symbol(Derived, Decl(checkInheritedProperty.ts, 3, 56))
15+
>BaseFactory : Symbol(BaseFactory, Decl(checkInheritedProperty.ts, 3, 13))
16+
17+
a = this.b
18+
>a : Symbol(Derived.a, Decl(checkInheritedProperty.ts, 5, 35))
19+
>this.b : Symbol(Derived.b, Decl(checkInheritedProperty.ts, 6, 14))
20+
>this : Symbol(Derived, Decl(checkInheritedProperty.ts, 3, 56))
21+
>b : Symbol(Derived.b, Decl(checkInheritedProperty.ts, 6, 14))
22+
23+
b = "abc"
24+
>b : Symbol(Derived.b, Decl(checkInheritedProperty.ts, 6, 14))
25+
}
26+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//// [tests/cases/compiler/checkInheritedProperty.ts] ////
2+
3+
=== checkInheritedProperty.ts ===
4+
class Base {
5+
>Base : Base
6+
}
7+
8+
declare const BaseFactory: new() => Base & { c: string }
9+
>BaseFactory : new () => Base & { c: string; }
10+
>c : string
11+
12+
class Derived extends BaseFactory {
13+
>Derived : Derived
14+
>BaseFactory : Base & { c: string; }
15+
16+
a = this.b
17+
>a : string
18+
>this.b : string
19+
>this : this
20+
>b : string
21+
22+
b = "abc"
23+
>b : string
24+
>"abc" : "abc"
25+
}
26+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// @strict: true
2+
// @noEmit: true
3+
4+
class Base {
5+
}
6+
7+
declare const BaseFactory: new() => Base & { c: string }
8+
9+
class Derived extends BaseFactory {
10+
a = this.b
11+
b = "abc"
12+
}

0 commit comments

Comments
 (0)