Skip to content

Commit 29378b2

Browse files
authored
Call back into getTypeOfFuncClassEnumModule in getTypeOfVariableOrParameterOrProperty if valueDeclaration is irregular kind (#20939)
1 parent 696b0f8 commit 29378b2

8 files changed

+67
-39
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4685,7 +4685,11 @@ namespace ts {
46854685
|| isIdentifier(declaration)
46864686
|| (isMethodDeclaration(declaration) && !isObjectLiteralMethod(declaration))
46874687
|| isMethodSignature(declaration)) {
4688-
// TODO: Mimics old behavior from incorrect usage of getWidenedTypeForVariableLikeDeclaration, but seems incorrect
4688+
4689+
// Symbol is property of some kind that is merged with something - should use `getTypeOfFuncClassEnumModule` and not `getTypeOfVariableOrParameterOrProperty`
4690+
if (symbol.flags & (SymbolFlags.Function | SymbolFlags.Method | SymbolFlags.Class | SymbolFlags.Enum | SymbolFlags.ValueModule)) {
4691+
return getTypeOfFuncClassEnumModule(symbol);
4692+
}
46894693
type = tryGetTypeFromEffectiveTypeNode(declaration) || anyType;
46904694
}
46914695
else if (isPropertyAssignment(declaration)) {
@@ -21647,7 +21651,7 @@ namespace ts {
2164721651
return;
2164821652
}
2164921653
const symbol = getSymbolOfNode(node);
21650-
const type = convertAutoToAny(getTypeOfVariableOrParameterOrProperty(symbol));
21654+
const type = convertAutoToAny(getTypeOfSymbol(symbol));
2165121655
if (node === symbol.valueDeclaration) {
2165221656
// Node is the primary declaration of the symbol, just validate the initializer
2165321657
// Don't validate for-in initializer as it is already an error

tests/baselines/reference/classWithDuplicateIdentifier.errors.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
tests/cases/compiler/classWithDuplicateIdentifier.ts(3,5): error TS2300: Duplicate identifier 'a'.
2+
tests/cases/compiler/classWithDuplicateIdentifier.ts(3,5): error TS2717: Subsequent property declarations must have the same type. Property 'a' must be of type '() => number', but here has type 'number'.
23
tests/cases/compiler/classWithDuplicateIdentifier.ts(6,5): error TS2300: Duplicate identifier 'b'.
34
tests/cases/compiler/classWithDuplicateIdentifier.ts(7,5): error TS2300: Duplicate identifier 'b'.
45
tests/cases/compiler/classWithDuplicateIdentifier.ts(11,5): error TS2300: Duplicate identifier 'c'.
56
tests/cases/compiler/classWithDuplicateIdentifier.ts(11,5): error TS2717: Subsequent property declarations must have the same type. Property 'c' must be of type 'number', but here has type 'string'.
67

78

8-
==== tests/cases/compiler/classWithDuplicateIdentifier.ts (5 errors) ====
9+
==== tests/cases/compiler/classWithDuplicateIdentifier.ts (6 errors) ====
910
class C {
1011
a(): number { return 0; } // error: duplicate identifier
1112
a: number;
1213
~
1314
!!! error TS2300: Duplicate identifier 'a'.
15+
~
16+
!!! error TS2717: Subsequent property declarations must have the same type. Property 'a' must be of type '() => number', but here has type 'number'.
1417
}
1518
class K {
1619
b: number; // error: duplicate identifier

tests/baselines/reference/classWithDuplicateIdentifier.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ class C {
33
>C : C
44

55
a(): number { return 0; } // error: duplicate identifier
6-
>a : number
6+
>a : () => number
77
>0 : 0
88

99
a: number;
10-
>a : number
10+
>a : () => number
1111
}
1212
class K {
1313
>K : K
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
tests/cases/compiler/methodSignatureHandledDeclarationKindForSymbol.ts(6,5): error TS2717: Subsequent property declarations must have the same type. Property 'bold' must be of type '() => string', but here has type 'string'.
2+
3+
4+
==== tests/cases/compiler/methodSignatureHandledDeclarationKindForSymbol.ts (1 errors) ====
5+
interface Foo {
6+
bold(): string;
7+
}
8+
9+
interface Foo {
10+
bold: string;
11+
~~~~
12+
!!! error TS2717: Subsequent property declarations must have the same type. Property 'bold' must be of type '() => string', but here has type 'string'.
13+
}
14+
15+

tests/baselines/reference/methodSignatureHandledDeclarationKindForSymbol.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ interface Foo {
33
>Foo : Foo
44

55
bold(): string;
6-
>bold : string
6+
>bold : () => string
77
}
88

99
interface Foo {
1010
>Foo : Foo
1111

1212
bold: string;
13-
>bold : string
13+
>bold : () => string
1414
}
1515

1616

tests/baselines/reference/multipleDeclarations.types

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ class X {
4343

4444
this.mistake = 'frankly, complete nonsense';
4545
>this.mistake = 'frankly, complete nonsense' : "frankly, complete nonsense"
46-
>this.mistake : any
46+
>this.mistake : () => void
4747
>this : this
48-
>mistake : any
48+
>mistake : () => void
4949
>'frankly, complete nonsense' : "frankly, complete nonsense"
5050
}
5151
m() {
5252
>m : () => void
5353
}
5454
mistake() {
55-
>mistake : any
55+
>mistake : () => void
5656
}
5757
}
5858
let x = new X();
@@ -62,11 +62,11 @@ let x = new X();
6262

6363
X.prototype.mistake = false;
6464
>X.prototype.mistake = false : false
65-
>X.prototype.mistake : any
65+
>X.prototype.mistake : () => void
6666
>X.prototype : X
6767
>X : typeof X
6868
>prototype : X
69-
>mistake : any
69+
>mistake : () => void
7070
>false : false
7171

7272
x.m();
@@ -76,15 +76,15 @@ x.m();
7676
>m : () => void
7777

7878
x.mistake;
79-
>x.mistake : any
79+
>x.mistake : () => void
8080
>x : X
81-
>mistake : any
81+
>mistake : () => void
8282

8383
class Y {
8484
>Y : Y
8585

8686
mistake() {
87-
>mistake : any
87+
>mistake : () => void
8888
}
8989
m() {
9090
>m : () => void
@@ -105,19 +105,19 @@ class Y {
105105

106106
this.mistake = 'even more nonsense';
107107
>this.mistake = 'even more nonsense' : "even more nonsense"
108-
>this.mistake : any
108+
>this.mistake : () => void
109109
>this : this
110-
>mistake : any
110+
>mistake : () => void
111111
>'even more nonsense' : "even more nonsense"
112112
}
113113
}
114114
Y.prototype.mistake = true;
115115
>Y.prototype.mistake = true : true
116-
>Y.prototype.mistake : any
116+
>Y.prototype.mistake : () => void
117117
>Y.prototype : Y
118118
>Y : typeof Y
119119
>prototype : Y
120-
>mistake : any
120+
>mistake : () => void
121121
>true : true
122122

123123
let y = new Y();
@@ -132,8 +132,8 @@ y.m();
132132
>m : () => void
133133

134134
y.mistake();
135-
>y.mistake() : any
136-
>y.mistake : any
135+
>y.mistake() : void
136+
>y.mistake : () => void
137137
>y : Y
138-
>mistake : any
138+
>mistake : () => void
139139

tests/baselines/reference/typeFromPropertyAssignmentOutOfOrder.symbols

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
=== tests/cases/conformance/salsa/index.js ===
22
Common.Item = class I {}
3+
>Common.Item : Symbol(Common.Item, Decl(index.js, 0, 0))
34
>Common : Symbol(Common, Decl(index.js, 0, 0), Decl(roots.js, 0, 3))
5+
>Item : Symbol(Common.Item, Decl(index.js, 0, 0))
46
>I : Symbol(I, Decl(index.js, 0, 13))
57

68
Common.Object = class extends Common.Item {}
9+
>Common.Object : Symbol(Common.Object, Decl(index.js, 0, 24))
710
>Common : Symbol(Common, Decl(index.js, 0, 0), Decl(roots.js, 0, 3))
11+
>Object : Symbol(Common.Object, Decl(index.js, 0, 24))
812
>Common.Item : Symbol(Common.Item, Decl(index.js, 0, 0))
913
>Common : Symbol(Common, Decl(index.js, 0, 0), Decl(roots.js, 0, 3))
1014
>Item : Symbol(Common.Item, Decl(index.js, 0, 0))
1115

1216
Workspace.Object = class extends Common.Object {}
17+
>Workspace.Object : Symbol(Workspace.Object, Decl(index.js, 1, 44))
1318
>Workspace : Symbol(Workspace, Decl(index.js, 1, 44), Decl(roots.js, 1, 3))
19+
>Object : Symbol(Workspace.Object, Decl(index.js, 1, 44))
1420
>Common.Object : Symbol(Common.Object, Decl(index.js, 0, 24))
1521
>Common : Symbol(Common, Decl(index.js, 0, 0), Decl(roots.js, 0, 3))
1622
>Object : Symbol(Common.Object, Decl(index.js, 0, 24))
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
=== tests/cases/conformance/salsa/index.js ===
22
Common.Item = class I {}
33
>Common.Item = class I {} : typeof I
4-
>Common.Item : any
5-
>Common : any
6-
>Item : any
4+
>Common.Item : typeof I
5+
>Common : typeof Common
6+
>Item : typeof I
77
>class I {} : typeof I
88
>I : typeof I
99

1010
Common.Object = class extends Common.Item {}
1111
>Common.Object = class extends Common.Item {} : typeof (Anonymous class)
12-
>Common.Object : any
13-
>Common : any
14-
>Object : any
12+
>Common.Object : typeof (Anonymous class)
13+
>Common : typeof Common
14+
>Object : typeof (Anonymous class)
1515
>class extends Common.Item {} : typeof (Anonymous class)
16-
>Common.Item : any
17-
>Common : any
18-
>Item : any
16+
>Common.Item : I
17+
>Common : typeof Common
18+
>Item : typeof I
1919

2020
Workspace.Object = class extends Common.Object {}
2121
>Workspace.Object = class extends Common.Object {} : typeof (Anonymous class)
22-
>Workspace.Object : any
23-
>Workspace : any
24-
>Object : any
22+
>Workspace.Object : typeof (Anonymous class)
23+
>Workspace : typeof Workspace
24+
>Object : typeof (Anonymous class)
2525
>class extends Common.Object {} : typeof (Anonymous class)
26-
>Common.Object : any
27-
>Common : any
28-
>Object : any
26+
>Common.Object : (Anonymous class)
27+
>Common : typeof Common
28+
>Object : typeof (Anonymous class)
2929

3030
/** @type {Workspace.Object} */
3131
var am;
3232
>am : (Anonymous class)
3333

3434
=== tests/cases/conformance/salsa/roots.js ===
3535
var Common = {};
36-
>Common : any
36+
>Common : typeof Common
3737
>{} : { [x: string]: any; }
3838

3939
var Workspace = {};
40-
>Workspace : any
40+
>Workspace : typeof Workspace
4141
>{} : { [x: string]: any; }
4242

0 commit comments

Comments
 (0)