Repository: alloy-framework/alloy
Pull request #141 (2025-05-01 19:14:09)

Fix type import from a package not adding `type` modifier


Patch:
diff --git a/.chronus/changes/fix-pkg-type-import-2025-4-1-19-14-32.md b/.chronus/changes/fix-pkg-type-import-2025-4-1-19-14-32.md
new file mode 100644
index 0000000..560b4b0
--- /dev/null
+++ b/.chronus/changes/fix-pkg-type-import-2025-4-1-19-14-32.md
@@ -0,0 +1,8 @@
+---
+# Change versionKind to one of: breaking, feature, fix, internal
+changeKind: fix
+packages:
+  - "@alloy-js/typescript"
+---
+
+Fix type import from a package not adding `type` modifier
diff --git a/packages/typescript/src/symbols/reference.ts b/packages/typescript/src/symbols/reference.ts
index a60e632..4c39389 100644
--- a/packages/typescript/src/symbols/reference.ts
+++ b/packages/typescript/src/symbols/reference.ts
@@ -85,7 +85,9 @@ export function ref(refkey: Refkey, options?: RefOptions): () => string {
         for (const refkey of importSymbol.refkeys) {
           if (module.exportedSymbols.has(refkey)) {
             localSymbol = untrack(() =>
-              sourceFile!.scope.addImport(importSymbol, module),
+              sourceFile!.scope.addImport(importSymbol, module, {
+                type: options?.type,
+              }),
             );
             break;
           }


Test patch:
diff --git a/packages/typescript/test/imports.test.tsx b/packages/typescript/test/imports.test.tsx
index 03568da..df8d768 100644
--- a/packages/typescript/test/imports.test.tsx
+++ b/packages/typescript/test/imports.test.tsx
@@ -11,6 +11,7 @@ import { describe, it } from "vitest";
 import * as ts from "../src/components/index.js";
 import { Reference } from "../src/components/Reference.js";
 import { TypeRefContext } from "../src/context/type-ref-context.js";
+import { createPackage } from "../src/create-package.js";
 import { tsNameConflictResolver } from "../src/name-conflict-resolver.js";
 import { assertFileContents } from "./utils.js";
 
@@ -492,6 +493,34 @@ describe("type imports", () => {
     });
   });
 
+  it("value reference from another file doesn't affect a type only reference", () => {
+    const { component, ClassA } = mkTestFile("test1.ts");
+    const res = render(
+      <Output>
+        {component}
+        <ts.SourceFile path="test2.ts">
+          type A = <Reference refkey={ClassA} type />
+        </ts.SourceFile>
+        <ts.SourceFile path="test3.ts">
+          class B extends <Reference refkey={ClassA} /> {"{}"}
+        </ts.SourceFile>
+      </Output>,
+    );
+
+    assertFileContents(res, {
+      "test2.ts": `
+      import type { ClassA } from "./test1.js";
+
+      type A = ClassA
+    `,
+      "test3.ts": `
+      import { ClassA } from "./test1.js";
+
+      class B extends ClassA {}
+    `,
+    });
+  });
+
   it("infer if a type reference from the typescript context", () => {
     const { component, TypeA } = mkTestFile("test1.ts");
     const res = render(
@@ -521,4 +550,32 @@ describe("type imports", () => {
     `,
     });
   });
+
+  it("reference from a package", () => {
+    const pkg1 = createPackage({
+      name: "test",
+      version: "^1.0.0",
+      descriptor: {
+        ".": {
+          named: ["Foo"],
+        },
+      },
+    });
+
+    const res = render(
+      <Output externals={[pkg1]}>
+        <ts.SourceFile path="test2.ts">
+          type A = <Reference refkey={pkg1.Foo} type />
+        </ts.SourceFile>
+      </Output>,
+    );
+
+    assertFileContents(res, {
+      "test2.ts": `
+      import type { Foo } from "test";
+
+      type A = Foo
+    `,
+    });
+  });
 });