Skip to content

Commit a423791

Browse files
authored
fix merging of function and derived class (microsoft#47170)
1 parent 20e86da commit a423791

File tree

5 files changed

+115
-3
lines changed

5 files changed

+115
-3
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9900,7 +9900,8 @@ namespace ts {
99009900
}
99019901

99029902
function getBaseTypeNodeOfClass(type: InterfaceType): ExpressionWithTypeArguments | undefined {
9903-
return getEffectiveBaseTypeNode(type.symbol.valueDeclaration as ClassLikeDeclaration);
9903+
const decl = getClassLikeDeclarationOfSymbol(type.symbol);
9904+
return decl && getEffectiveBaseTypeNode(decl);
99049905
}
99059906

99069907
function getConstructorsForTypeArguments(type: Type, typeArgumentNodes: readonly TypeNode[] | undefined, location: Node): readonly Signature[] {
@@ -9926,8 +9927,8 @@ namespace ts {
99269927
*/
99279928
function getBaseConstructorTypeOfClass(type: InterfaceType): Type {
99289929
if (!type.resolvedBaseConstructorType) {
9929-
const decl = type.symbol.valueDeclaration as ClassLikeDeclaration;
9930-
const extended = getEffectiveBaseTypeNode(decl);
9930+
const decl = getClassLikeDeclarationOfSymbol(type.symbol);
9931+
const extended = decl && getEffectiveBaseTypeNode(decl);
99319932
const baseTypeNode = getBaseTypeNodeOfClass(type);
99329933
if (!baseTypeNode) {
99339934
return type.resolvedBaseConstructorType = undefinedType;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [classFunctionMerging2.ts]
2+
declare abstract class A {
3+
constructor(p: number);
4+
a: number;
5+
}
6+
7+
declare function B(p: string): B;
8+
declare class B extends A {
9+
constructor(p: string);
10+
b: number;
11+
}
12+
13+
let b = new B("Hey")
14+
console.log(b.a)
15+
16+
//// [classFunctionMerging2.js]
17+
var b = new B("Hey");
18+
console.log(b.a);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
=== tests/cases/compiler/classFunctionMerging2.ts ===
2+
declare abstract class A {
3+
>A : Symbol(A, Decl(classFunctionMerging2.ts, 0, 0))
4+
5+
constructor(p: number);
6+
>p : Symbol(p, Decl(classFunctionMerging2.ts, 1, 16))
7+
8+
a: number;
9+
>a : Symbol(A.a, Decl(classFunctionMerging2.ts, 1, 27))
10+
}
11+
12+
declare function B(p: string): B;
13+
>B : Symbol(B, Decl(classFunctionMerging2.ts, 3, 1), Decl(classFunctionMerging2.ts, 5, 33))
14+
>p : Symbol(p, Decl(classFunctionMerging2.ts, 5, 19))
15+
>B : Symbol(B, Decl(classFunctionMerging2.ts, 3, 1), Decl(classFunctionMerging2.ts, 5, 33))
16+
17+
declare class B extends A {
18+
>B : Symbol(B, Decl(classFunctionMerging2.ts, 3, 1), Decl(classFunctionMerging2.ts, 5, 33))
19+
>A : Symbol(A, Decl(classFunctionMerging2.ts, 0, 0))
20+
21+
constructor(p: string);
22+
>p : Symbol(p, Decl(classFunctionMerging2.ts, 7, 16))
23+
24+
b: number;
25+
>b : Symbol(B.b, Decl(classFunctionMerging2.ts, 7, 27))
26+
}
27+
28+
let b = new B("Hey")
29+
>b : Symbol(b, Decl(classFunctionMerging2.ts, 11, 3))
30+
>B : Symbol(B, Decl(classFunctionMerging2.ts, 3, 1), Decl(classFunctionMerging2.ts, 5, 33))
31+
32+
console.log(b.a)
33+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
34+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
35+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
36+
>b.a : Symbol(A.a, Decl(classFunctionMerging2.ts, 1, 27))
37+
>b : Symbol(b, Decl(classFunctionMerging2.ts, 11, 3))
38+
>a : Symbol(A.a, Decl(classFunctionMerging2.ts, 1, 27))
39+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
=== tests/cases/compiler/classFunctionMerging2.ts ===
2+
declare abstract class A {
3+
>A : A
4+
5+
constructor(p: number);
6+
>p : number
7+
8+
a: number;
9+
>a : number
10+
}
11+
12+
declare function B(p: string): B;
13+
>B : typeof B
14+
>p : string
15+
16+
declare class B extends A {
17+
>B : B
18+
>A : A
19+
20+
constructor(p: string);
21+
>p : string
22+
23+
b: number;
24+
>b : number
25+
}
26+
27+
let b = new B("Hey")
28+
>b : B
29+
>new B("Hey") : B
30+
>B : typeof B
31+
>"Hey" : "Hey"
32+
33+
console.log(b.a)
34+
>console.log(b.a) : void
35+
>console.log : (...data: any[]) => void
36+
>console : Console
37+
>log : (...data: any[]) => void
38+
>b.a : number
39+
>b : B
40+
>a : number
41+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
declare abstract class A {
2+
constructor(p: number);
3+
a: number;
4+
}
5+
6+
declare function B(p: string): B;
7+
declare class B extends A {
8+
constructor(p: string);
9+
b: number;
10+
}
11+
12+
let b = new B("Hey")
13+
console.log(b.a)

0 commit comments

Comments
 (0)