Repository: alloy-framework/alloy
Pull request #15 (2024-08-28 20:02:45)

Function Declarartion can take Children as return type
Function Declarartion can take Children as return type instead of just a string

pkg-pr-new[bot]: 
_commit: <a href="https://github.com/alloy-framework/alloy/runs/29388295772"><code>d588daf</code></a>_


<details><summary><b>@alloy-js/babel-plugin-alloy</b></summary><p>

```
pnpm add https://pkg.pr.new/alloy-framework/alloy/@alloy-js/babel-plugin-alloy@15
```

</p></details>
      
    

<details><summary><b>@alloy-js/babel-plugin-jsx-dom-expressions</b></summary><p>

```
pnpm add https://pkg.pr.new/alloy-framework/alloy/@alloy-js/babel-plugin-jsx-dom-expressions@15
```

</p></details>
      
    

<details><summary><b>babel-preset-alloy</b></summary><p>

```
pnpm add https://pkg.pr.new/alloy-framework/alloy/babel-preset-alloy@15
```

</p></details>
      
    

<details><summary><b>@alloy-js/core</b></summary><p>

```
pnpm add https://pkg.pr.new/alloy-framework/alloy/@alloy-js/core@15
```

</p></details>
      
    

<details><summary><b>@alloy-js/java</b></summary><p>

```
pnpm add https://pkg.pr.new/alloy-framework/alloy/@alloy-js/java@15
```

</p></details>
      
    

<details><summary><b>@alloy-js/prettier-plugin-alloy</b></summary><p>

```
pnpm add https://pkg.pr.new/alloy-framework/alloy/@alloy-js/prettier-plugin-alloy@15
```

</p></details>
      
    

<details><summary><b>@alloy-js/typescript</b></summary><p>

```
pnpm add https://pkg.pr.new/alloy-framework/alloy/@alloy-js/typescript@15
```

</p></details>
      
    

[Open in Stackblitz](https://pkg.pr.new/template/bae130d0-4ce3-48ce-a092-c2b7c5efa75a)


Patch:
diff --git a/packages/typescript/src/components/FunctionDeclaration.tsx b/packages/typescript/src/components/FunctionDeclaration.tsx
index a7c170f..fa08746 100644
--- a/packages/typescript/src/components/FunctionDeclaration.tsx
+++ b/packages/typescript/src/components/FunctionDeclaration.tsx
@@ -1,5 +1,6 @@
 import {
   childrenArray,
+  code,
   findKeyedChild,
   findUnkeyedChildren,
   mapJoin,
@@ -30,7 +31,7 @@ export interface FunctionDeclarationProps
   extends Omit<DeclarationProps, "nameKind"> {
   async?: boolean;
   parameters?: Record<string, Children | ParameterDescriptor>;
-  returnType?: string;
+  returnType?: Children;
   children?: Children;
 }
 
@@ -63,11 +64,11 @@ export function FunctionDeclaration(props: FunctionDeclarationProps) {
 }
 
 function getReturnType(
-  returnType: string | undefined,
+  returnType: Children | undefined,
   options: { async?: boolean } = { async: false },
 ) {
   if (returnType) {
-    return options.async ? `Promise<${returnType}>` : returnType;
+    return options.async ? code`Promise<${returnType}>` : returnType;
   }
 }
 export interface FunctionParametersProps {


Test patch:
diff --git a/packages/typescript/test/function-declaration.test.tsx b/packages/typescript/test/function-declaration.test.tsx
index e16be73..58649bd 100644
--- a/packages/typescript/test/function-declaration.test.tsx
+++ b/packages/typescript/test/function-declaration.test.tsx
@@ -1,4 +1,4 @@
-import { refkey } from "@alloy-js/core";
+import { Props, refkey } from "@alloy-js/core";
 import { d } from "@alloy-js/core/testing";
 import { describe, expect, it } from "vitest";
 import { FunctionDeclaration, VarDeclaration } from "../src/index.js";
@@ -58,6 +58,21 @@ it("can be an async function with returnType", () => {
   `);
 });
 
+it("can be an async function with returnType element", () => {
+  function Foo(_props?: Props) {
+    return <>Foo</>;
+  }
+  expect(
+    toSourceText(
+      <FunctionDeclaration async export name="foo" returnType={<Foo />}/>,
+    ),
+  ).toBe(d`
+    export async function foo(): Promise<Foo> {
+      
+    }
+  `);
+});
+
 it("supports parameters by element", () => {
   const decl =
     <FunctionDeclaration name="foo">