Skip to content

Commit 42f893f

Browse files
authored
Mark the inherited any-based index signature so it can be elided in declaration emit (#60680)
1 parent 676d329 commit 42f893f

File tree

5 files changed

+76
-2
lines changed

5 files changed

+76
-2
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,6 +2151,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
21512151
var silentNeverSignature = createSignature(/*declaration*/ undefined, /*typeParameters*/ undefined, /*thisParameter*/ undefined, emptyArray, silentNeverType, /*resolvedTypePredicate*/ undefined, 0, SignatureFlags.None);
21522152

21532153
var enumNumberIndexInfo = createIndexInfo(numberType, stringType, /*isReadonly*/ true);
2154+
var anyBaseTypeIndexInfo = createIndexInfo(stringType, anyType, /*isReadonly*/ false);
21542155

21552156
var iterationTypesCache = new Map<string, IterationTypes>(); // cache for common IterationTypes instances
21562157
var noIterationTypes: IterationTypes = {
@@ -13454,7 +13455,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1345413455
addInheritedMembers(members, getPropertiesOfType(instantiatedBaseType));
1345513456
callSignatures = concatenate(callSignatures, getSignaturesOfType(instantiatedBaseType, SignatureKind.Call));
1345613457
constructSignatures = concatenate(constructSignatures, getSignaturesOfType(instantiatedBaseType, SignatureKind.Construct));
13457-
const inheritedIndexInfos = instantiatedBaseType !== anyType ? getIndexInfosOfType(instantiatedBaseType) : [createIndexInfo(stringType, anyType, /*isReadonly*/ false)];
13458+
const inheritedIndexInfos = instantiatedBaseType !== anyType ? getIndexInfosOfType(instantiatedBaseType) : [anyBaseTypeIndexInfo];
1345813459
indexInfos = concatenate(indexInfos, filter(inheritedIndexInfos, info => !findIndexInfo(indexInfos, info.keyType)));
1345913460
}
1346013461
}
@@ -13986,7 +13987,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1398613987
addInheritedMembers(members, getPropertiesOfType(baseConstructorType));
1398713988
}
1398813989
else if (baseConstructorType === anyType) {
13989-
baseConstructorIndexInfo = createIndexInfo(stringType, anyType, /*isReadonly*/ false);
13990+
baseConstructorIndexInfo = anyBaseTypeIndexInfo;
1399013991
}
1399113992
}
1399213993

@@ -50781,6 +50782,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
5078150782
result ||= [];
5078250783
for (const info of infoList!) {
5078350784
if (info.declaration) continue;
50785+
if (info === anyBaseTypeIndexInfo) continue; // inherited, but looks like a late-bound signature because it has no declarations
5078450786
const node = nodeBuilder.indexInfoToIndexSignatureDeclaration(info, enclosing, flags, internalFlags, tracker);
5078550787
if (node && infoList === staticInfos) {
5078650788
(((node as Mutable<typeof node>).modifiers ||= factory.createNodeArray()) as MutableNodeArray<Modifier>).unshift(factory.createModifier(SyntaxKind.StaticKeyword));
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//// [tests/cases/compiler/declarationEmitClassInherritsAny.ts] ////
2+
3+
//// [declarationEmitClassInherritsAny.ts]
4+
const anyThing = class {} as any;
5+
export class Foo extends anyThing {}
6+
7+
//// [declarationEmitClassInherritsAny.js]
8+
"use strict";
9+
var __extends = (this && this.__extends) || (function () {
10+
var extendStatics = function (d, b) {
11+
extendStatics = Object.setPrototypeOf ||
12+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
13+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
14+
return extendStatics(d, b);
15+
};
16+
return function (d, b) {
17+
if (typeof b !== "function" && b !== null)
18+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
19+
extendStatics(d, b);
20+
function __() { this.constructor = d; }
21+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
22+
};
23+
})();
24+
Object.defineProperty(exports, "__esModule", { value: true });
25+
exports.Foo = void 0;
26+
var anyThing = /** @class */ (function () {
27+
function class_1() {
28+
}
29+
return class_1;
30+
}());
31+
var Foo = /** @class */ (function (_super) {
32+
__extends(Foo, _super);
33+
function Foo() {
34+
return _super !== null && _super.apply(this, arguments) || this;
35+
}
36+
return Foo;
37+
}(anyThing));
38+
exports.Foo = Foo;
39+
40+
41+
//// [declarationEmitClassInherritsAny.d.ts]
42+
declare const anyThing: any;
43+
export declare class Foo extends anyThing {
44+
}
45+
export {};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [tests/cases/compiler/declarationEmitClassInherritsAny.ts] ////
2+
3+
=== declarationEmitClassInherritsAny.ts ===
4+
const anyThing = class {} as any;
5+
>anyThing : Symbol(anyThing, Decl(declarationEmitClassInherritsAny.ts, 0, 5))
6+
7+
export class Foo extends anyThing {}
8+
>Foo : Symbol(Foo, Decl(declarationEmitClassInherritsAny.ts, 0, 33))
9+
>anyThing : Symbol(anyThing, Decl(declarationEmitClassInherritsAny.ts, 0, 5))
10+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//// [tests/cases/compiler/declarationEmitClassInherritsAny.ts] ////
2+
3+
=== declarationEmitClassInherritsAny.ts ===
4+
const anyThing = class {} as any;
5+
>anyThing : any
6+
>class {} as any : any
7+
>class {} : typeof (Anonymous class)
8+
> : ^^^^^^^^^^^^^^^^^^^^^^^^
9+
10+
export class Foo extends anyThing {}
11+
>Foo : Foo
12+
> : ^^^
13+
>anyThing : any
14+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// @declaration: true
2+
const anyThing = class {} as any;
3+
export class Foo extends anyThing {}

0 commit comments

Comments
 (0)