From 88d787c4ab9101ad7f2e2705f7ec781c98582fa5 Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Sat, 2 Mar 2024 18:31:49 -0700 Subject: [PATCH] Fix links to constructed enum references Resolves #2508 --- CHANGELOG.md | 4 ++++ src/lib/converter/types.ts | 8 ++++++-- src/test/converter2/issues/gh2508.ts | 16 ++++++++++++++++ src/test/issues.c2.test.ts | 8 ++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 src/test/converter2/issues/gh2508.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b82041e0..aaf4a5418 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Unreleased +### Bug Fixes + +- Constructed references to enum types will be properly linked with `@interface`, #2508. + ## v0.25.9 (2024-02-26) ### Features diff --git a/src/lib/converter/types.ts b/src/lib/converter/types.ts index 7ffe2e183..d1d19ee49 100644 --- a/src/lib/converter/types.ts +++ b/src/lib/converter/types.ts @@ -724,8 +724,12 @@ const referenceConverter: TypeConverter< ); return type; }, - convertType(context, type) { - const symbol = type.aliasSymbol ?? type.getSymbol(); + convertType(context, type, node) { + // typeName.symbol handles the case where this is a union which happens to refer + // to an enumeration. TS doesn't put the symbol on the type for some reason, but + // does add it to the constructed type node. + const symbol = + type.aliasSymbol ?? type.getSymbol() ?? node.typeName.symbol; if (!symbol) { // This happens when we get a reference to a type parameter // created within a mapped type, `K` in: `{ [K in T]: string }` diff --git a/src/test/converter2/issues/gh2508.ts b/src/test/converter2/issues/gh2508.ts new file mode 100644 index 000000000..2cce64437 --- /dev/null +++ b/src/test/converter2/issues/gh2508.ts @@ -0,0 +1,16 @@ +export enum Color { + BLUE = "Blue", + RED = "Red", +} + +type TypeOf = { + [K in keyof T]: T[K][keyof T[K]]; +}; + +type Foo = { + color: typeof Color; +}; + +/** @interface */ +export type Bar = TypeOf; +// ^? diff --git a/src/test/issues.c2.test.ts b/src/test/issues.c2.test.ts index 24c1b9b84..99e4021f5 100644 --- a/src/test/issues.c2.test.ts +++ b/src/test/issues.c2.test.ts @@ -1412,4 +1412,12 @@ describe("Issue Tests", () => { // }>(object: I): void equal(type?.toString(), "Value & Object"); }); + + it("Handles constructed references to enumeration types, #2508", () => { + const project = convert(); + const refl = query(project, "Bar.color"); + equal(refl.type?.type, "reference"); + equal(refl.type.toString(), "Color"); + equal(refl.type.reflection?.id, query(project, "Color").id); + }); });