Skip to content

Commit d6e6fa7

Browse files
TypeScript Botweswigham
TypeScript Bot
andauthored
Cherry-pick PR microsoft#44129 into release-4.3 (microsoft#44240)
Component commits: 7a9854c Ensure static index signatures have an errorNode available 626b431 Merge branch 'master' into error-node-static-index-signatures a8327a0 Lookup static index signature declarations in the right symbol table, stop checking prototype props Co-authored-by: Wesley Wigham <[email protected]>
1 parent 27d6e44 commit d6e6fa7

6 files changed

+79
-4
lines changed

src/compiler/checker.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -12531,6 +12531,11 @@ namespace ts {
1253112531
return indexSymbol && getIndexDeclarationOfIndexSymbol(indexSymbol, kind);
1253212532
}
1253312533

12534+
function getIndexDeclarationOfSymbolTable(symbolTable: SymbolTable | undefined, kind: IndexKind): IndexSignatureDeclaration | undefined {
12535+
const indexSymbol = symbolTable && getIndexSymbolFromSymbolTable(symbolTable);
12536+
return indexSymbol && getIndexDeclarationOfIndexSymbol(indexSymbol, kind);
12537+
}
12538+
1253412539
function getIndexDeclarationOfIndexSymbol(indexSymbol: Symbol, kind: IndexKind): IndexSignatureDeclaration | undefined {
1253512540
const syntaxKind = kind === IndexKind.Number ? SyntaxKind.NumberKeyword : SyntaxKind.StringKeyword;
1253612541
if (indexSymbol?.declarations) {
@@ -36773,15 +36778,16 @@ namespace ts {
3677336778
}
3677436779
}
3677536780

36776-
function checkIndexConstraints(type: Type) {
36777-
const declaredNumberIndexer = getIndexDeclarationOfSymbol(type.symbol, IndexKind.Number);
36778-
const declaredStringIndexer = getIndexDeclarationOfSymbol(type.symbol, IndexKind.String);
36781+
function checkIndexConstraints(type: Type, isStatic?: boolean) {
36782+
const declaredNumberIndexer = getIndexDeclarationOfSymbolTable(isStatic ? type.symbol?.exports : type.symbol?.members, IndexKind.Number);
36783+
const declaredStringIndexer = getIndexDeclarationOfSymbolTable(isStatic ? type.symbol?.exports : type.symbol?.members, IndexKind.String);
3677936784

3678036785
const stringIndexType = getIndexTypeOfType(type, IndexKind.String);
3678136786
const numberIndexType = getIndexTypeOfType(type, IndexKind.Number);
3678236787

3678336788
if (stringIndexType || numberIndexType) {
3678436789
forEach(getPropertiesOfObjectType(type), prop => {
36790+
if (isStatic && prop.flags & SymbolFlags.Prototype) return;
3678536791
const propType = getTypeOfSymbol(prop);
3678636792
checkIndexConstraintForProperty(prop, propType, type, declaredStringIndexer, stringIndexType, IndexKind.String);
3678736793
checkIndexConstraintForProperty(prop, propType, type, declaredNumberIndexer, numberIndexType, IndexKind.Number);
@@ -37160,7 +37166,7 @@ namespace ts {
3716037166

3716137167
if (produceDiagnostics) {
3716237168
checkIndexConstraints(type);
37163-
checkIndexConstraints(staticType);
37169+
checkIndexConstraints(staticType, /*isStatic*/ true);
3716437170
checkTypeForDuplicateIndexSignatures(node);
3716537171
checkPropertyInitialization(node);
3716637172
}
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [staticIndexSignatureAndNormalIndexSignature.ts]
2+
class Foo {
3+
[p: string]: any;
4+
static [p: string]: number;
5+
}
6+
7+
//// [staticIndexSignatureAndNormalIndexSignature.js]
8+
var Foo = /** @class */ (function () {
9+
function Foo() {
10+
}
11+
return Foo;
12+
}());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/compiler/staticIndexSignatureAndNormalIndexSignature.ts ===
2+
class Foo {
3+
>Foo : Symbol(Foo, Decl(staticIndexSignatureAndNormalIndexSignature.ts, 0, 0))
4+
5+
[p: string]: any;
6+
>p : Symbol(p, Decl(staticIndexSignatureAndNormalIndexSignature.ts, 1, 5))
7+
8+
static [p: string]: number;
9+
>p : Symbol(p, Decl(staticIndexSignatureAndNormalIndexSignature.ts, 2, 12))
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/compiler/staticIndexSignatureAndNormalIndexSignature.ts ===
2+
class Foo {
3+
>Foo : Foo
4+
5+
[p: string]: any;
6+
>p : string
7+
8+
static [p: string]: number;
9+
>p : string
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Foo {
2+
[p: string]: any;
3+
static [p: string]: number;
4+
}

0 commit comments

Comments
 (0)