Skip to content

Commit 766fe9e

Browse files
committed
Fix references to mapped type aliases
Resolves #2954
1 parent ea16fb7 commit 766fe9e

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
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+
- References to type aliases defined as mapped types will now correctly create a reference to the type alias, #2954.
10+
711
## v0.28.4 (2025-05-04)
812

913
### Features

src/lib/converter/types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,17 @@ const referenceConverter: TypeConverter<
792792
return ref;
793793
}
794794

795+
// #2954 mapped type aliases are special! The type that we have here will
796+
// not point at the type alias which names it like we want, but instead at
797+
// the mapped type instantiation. Fall back to converting via the original
798+
// type node to avoid creating a reference which points to the mapped type.
799+
if (
800+
originalNode && ts.isTypeReferenceNode(originalNode) && isObjectType(type) &&
801+
type.objectFlags & ts.ObjectFlags.Mapped
802+
) {
803+
return referenceConverter.convert(context, originalNode);
804+
}
805+
795806
let name: string;
796807
if (ts.isIdentifier(node.typeName)) {
797808
name = node.typeName.text;

src/test/converter2/issues/gh2954.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export type AliasA = Readonly<Record<string, string>>;
2+
3+
export type AliasB<T> = Readonly<Record<string, T>>;
4+
5+
export type AliasC = Readonly<{}>;
6+
7+
export interface InterfaceA {
8+
propertyA: AliasA;
9+
propertyB: AliasB<string>;
10+
propertyC: AliasC;
11+
}

src/test/issues.c2.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,4 +2133,11 @@ describe("Issue Tests", () => {
21332133
const project = convert();
21342134
equal(query(project, "Test").type?.toString(), "() => Promise<any>");
21352135
});
2136+
2137+
it("#2954 handles Readonly with Record type", () => {
2138+
const project = convert();
2139+
equal(query(project, "InterfaceA.propertyA").type?.toString(), "AliasA");
2140+
equal(query(project, "InterfaceA.propertyB").type?.toString(), "AliasB<string>");
2141+
equal(query(project, "InterfaceA.propertyC").type?.toString(), "AliasC");
2142+
});
21362143
});

0 commit comments

Comments
 (0)