Skip to content

Commit a8327a0

Browse files
committed
Lookup static index signature declarations in the right symbol table, stop checking prototype props
1 parent 626b431 commit a8327a0

5 files changed

+46
-9
lines changed

src/compiler/checker.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12605,6 +12605,11 @@ namespace ts {
1260512605
return indexSymbol && getIndexDeclarationOfIndexSymbol(indexSymbol, kind);
1260612606
}
1260712607

12608+
function getIndexDeclarationOfSymbolTable(symbolTable: SymbolTable | undefined, kind: IndexKind): IndexSignatureDeclaration | undefined {
12609+
const indexSymbol = symbolTable && getIndexSymbolFromSymbolTable(symbolTable);
12610+
return indexSymbol && getIndexDeclarationOfIndexSymbol(indexSymbol, kind);
12611+
}
12612+
1260812613
function getIndexDeclarationOfIndexSymbol(indexSymbol: Symbol, kind: IndexKind): IndexSignatureDeclaration | undefined {
1260912614
const syntaxKind = kind === IndexKind.Number ? SyntaxKind.NumberKeyword : SyntaxKind.StringKeyword;
1261012615
if (indexSymbol?.declarations) {
@@ -36868,15 +36873,16 @@ namespace ts {
3686836873
}
3686936874
}
3687036875

36871-
function checkIndexConstraints(type: Type) {
36872-
const declaredNumberIndexer = getIndexDeclarationOfSymbol(type.symbol, IndexKind.Number);
36873-
const declaredStringIndexer = getIndexDeclarationOfSymbol(type.symbol, IndexKind.String);
36876+
function checkIndexConstraints(type: Type, isStatic?: boolean) {
36877+
const declaredNumberIndexer = getIndexDeclarationOfSymbolTable(isStatic ? type.symbol?.exports : type.symbol?.members, IndexKind.Number);
36878+
const declaredStringIndexer = getIndexDeclarationOfSymbolTable(isStatic ? type.symbol?.exports : type.symbol?.members, IndexKind.String);
3687436879

3687536880
const stringIndexType = getIndexTypeOfType(type, IndexKind.String);
3687636881
const numberIndexType = getIndexTypeOfType(type, IndexKind.Number);
3687736882

3687836883
if (stringIndexType || numberIndexType) {
3687936884
forEach(getPropertiesOfObjectType(type), prop => {
36885+
if (isStatic && prop.flags & SymbolFlags.Prototype) return;
3688036886
const propType = getTypeOfSymbol(prop);
3688136887
checkIndexConstraintForProperty(prop, propType, type, declaredStringIndexer, stringIndexType, IndexKind.String);
3688236888
checkIndexConstraintForProperty(prop, propType, type, declaredNumberIndexer, numberIndexType, IndexKind.Number);
@@ -36906,11 +36912,6 @@ namespace ts {
3690636912
const someBaseTypeHasBothIndexers = forEach(getBaseTypes(type as InterfaceType), base => getIndexTypeOfType(base, IndexKind.String) && getIndexTypeOfType(base, IndexKind.Number));
3690736913
errorNode = someBaseTypeHasBothIndexers || !type.symbol.declarations ? undefined : type.symbol.declarations[0];
3690836914
}
36909-
if (!errorNode) {
36910-
// `getIndexDeclarationOfSymbol` does not return the declarations for static index signatures, since they
36911-
// come from the __index symbol in the `exports` table of the symbol, and not the `members` table
36912-
errorNode = getIndexInfoOfType(type, IndexKind.Number)?.declaration || getIndexInfoOfType(type, IndexKind.String)?.declaration;
36913-
}
3691436915
}
3691536916

3691636917
if (errorNode && !isTypeAssignableTo(numberIndexType!, stringIndexType!)) { // TODO: GH#18217
@@ -37260,7 +37261,7 @@ namespace ts {
3726037261

3726137262
if (produceDiagnostics) {
3726237263
checkIndexConstraints(type);
37263-
checkIndexConstraints(staticType);
37264+
checkIndexConstraints(staticType, /*isStatic*/ true);
3726437265
checkTypeForDuplicateIndexSignatures(node);
3726537266
checkPropertyInitialization(node);
3726637267
}
Lines changed: 12 additions & 0 deletions
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+
}());
Lines changed: 10 additions & 0 deletions
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+
}
Lines changed: 10 additions & 0 deletions
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+
}
Lines changed: 4 additions & 0 deletions
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)