Skip to content

Commit 7a9854c

Browse files
committed
Ensure static index signatures have an errorNode available
1 parent 9eab334 commit 7a9854c

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36898,6 +36898,11 @@ namespace ts {
3689836898
const someBaseTypeHasBothIndexers = forEach(getBaseTypes(<InterfaceType>type), base => getIndexTypeOfType(base, IndexKind.String) && getIndexTypeOfType(base, IndexKind.Number));
3689936899
errorNode = someBaseTypeHasBothIndexers || !type.symbol.declarations ? undefined : type.symbol.declarations[0];
3690036900
}
36901+
if (!errorNode) {
36902+
// `getIndexDeclarationOfSymbol` does not return the declarations for static index signatures, since they
36903+
// come from the __index symbol in the `exports` table of the symbol, and not the `members` table
36904+
errorNode = getIndexInfoOfType(type, IndexKind.Number)?.declaration || getIndexInfoOfType(type, IndexKind.String)?.declaration;
36905+
}
3690136906
}
3690236907

3690336908
if (errorNode && !isTypeAssignableTo(numberIndexType!, stringIndexType!)) { // TODO: GH#18217
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
tests/cases/conformance/classes/staticIndexSignature/staticIndexSignature3.ts(12,5): error TS2413: Numeric index type '1' is not assignable to string index type 'boolean'.
2+
3+
4+
==== tests/cases/conformance/classes/staticIndexSignature/staticIndexSignature3.ts (1 errors) ====
5+
class B {
6+
static readonly [s: string]: number;
7+
static readonly [s: number]: 42 | 233
8+
}
9+
10+
class D extends B {
11+
static readonly [s: string]: number
12+
}
13+
14+
class ED extends D {
15+
static readonly [s: string]: boolean
16+
static readonly [s: number]: 1
17+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18+
!!! error TS2413: Numeric index type '1' is not assignable to string index type 'boolean'.
19+
}
20+
21+
class DD extends D {
22+
static readonly [s: string]: 421
23+
}
24+
25+
const a = B["f"];
26+
const b = B[42];
27+
const c = D["f"]
28+
const d = D[42]
29+
const e = ED["f"]
30+
const f = ED[42]
31+
const g = DD["f"]
32+
const h = DD[42]
33+

0 commit comments

Comments
 (0)