Skip to content

Commit 79a3310

Browse files
committed
Fix #2917
1 parent 3a3976d commit 79a3310

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ title: Changelog
44

55
## Unreleased
66

7+
### Bug Fixes
8+
9+
- API: `toString` on types containing index signatures now behave correctly, #2917.
10+
711
## v0.28.1 (2025-03-20)
812

913
### Features

src/lib/models/types.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,19 +1098,19 @@ export class ReflectionType extends Type {
10981098

10991099
protected override getTypeString() {
11001100
const parts: string[] = [];
1101-
const sigs = this.declaration.getAllSignatures();
1101+
const sigs = this.declaration.getNonIndexSignatures();
11021102
for (const sig of sigs) {
11031103
parts.push(sigStr(sig, ": "));
11041104
}
11051105

1106-
if (this.declaration.children) {
1107-
for (const p of this.declaration.children) {
1108-
parts.push(`${p.name}${propertySep(p)} ${typeStr(p.type)}`);
1109-
}
1110-
return `{ ${parts.join("; ")} }`;
1106+
for (const p of this.declaration.children || []) {
1107+
parts.push(`${p.name}${propertySep(p)} ${typeStr(p.type)}`);
1108+
}
1109+
for (const s of this.declaration.indexSignatures || []) {
1110+
parts.push(sigStr(s, ": ", "[]"));
11111111
}
11121112

1113-
if (sigs.length === 1) {
1113+
if (sigs.length === 1 && parts.length === 1) {
11141114
return sigStr(sigs[0], " => ");
11151115
}
11161116

@@ -1486,11 +1486,11 @@ function typeStr(type: Type | undefined) {
14861486
return type?.toString() ?? "any";
14871487
}
14881488

1489-
function sigStr(sig: SignatureReflection, sep: string) {
1489+
function sigStr(sig: SignatureReflection, sep: string, brackets = "()") {
14901490
const params = joinArray(
14911491
sig.parameters,
14921492
", ",
14931493
(p) => `${p.name}${propertySep(p)} ${typeStr(p.type)}`,
14941494
);
1495-
return `(${params})${sep}${typeStr(sig.type)}`;
1495+
return `${brackets[0]}${params}${brackets[1]}${sep}${typeStr(sig.type)}`;
14961496
}

src/test/converter2/issues/gh2917.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export interface Foo {
2+
data: {
3+
[key: string]: any;
4+
};
5+
mixed: {
6+
(): string;
7+
a: string;
8+
[key: string]: any;
9+
};
10+
}

src/test/issues.c2.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,4 +2042,12 @@ describe("Issue Tests", () => {
20422042
const exp = query(project, "export=");
20432043
equal(exp.type?.toString(), "never[]");
20442044
});
2045+
2046+
it("#2917 stringifies index signatures", () => {
2047+
const project = convert();
2048+
const data = query(project, "Foo.data");
2049+
equal(data.type?.toString(), "{ [key: string]: any }");
2050+
const mixed = query(project, "Foo.mixed");
2051+
equal(mixed.type?.toString(), "{ (): string; a: string; [key: string]: any }");
2052+
});
20452053
});

0 commit comments

Comments
 (0)