Skip to content

Commit 36c1605

Browse files
authored
Fix type context for fn (#322)
1 parent b62a51a commit 36c1605

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
changeKind: fix
3+
packages:
4+
- "@alloy-js/typescript"
5+
---
6+
7+
Fix function return types not being in type context.

packages/typescript/src/components/CallSignature.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
TypeParameterDescriptor,
55
} from "../parameter-descriptor.js";
66
import { FunctionDeclaration } from "./FunctionDeclaration.jsx";
7+
import { TypeRefContext } from "./TypeRefContext.jsx";
78

89
export interface CallSignatureProps {
910
/**
@@ -57,7 +58,10 @@ export function CallSignature(props: CallSignatureProps) {
5758
<FunctionDeclaration.Parameters parameters={props.parameters} />
5859
);
5960

60-
const sReturnType = props.returnType ? <>: {props.returnType}</> : undefined;
61+
const sReturnType =
62+
props.returnType ?
63+
<TypeRefContext>: {props.returnType}</TypeRefContext>
64+
: undefined;
6165

6266
return (
6367
<>

packages/typescript/test/function-declaration.test.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
List,
33
memberRefkey,
44
namekey,
5+
Output,
56
Props,
67
refkey,
78
StatementList,
@@ -14,6 +15,8 @@ import {
1415
FunctionDeclaration,
1516
InterfaceDeclaration,
1617
InterfaceMember,
18+
SourceFile,
19+
TypeDeclaration,
1720
VarDeclaration,
1821
} from "../src/index.js";
1922
import { ParameterDescriptor } from "../src/parameter-descriptor.js";
@@ -386,3 +389,37 @@ describe("symbols", () => {
386389
`);
387390
});
388391
});
392+
393+
it("has parameters and return type in type ref context", () => {
394+
const i1 = namekey("iface1");
395+
const i2 = namekey("iface2");
396+
const template = (
397+
<Output>
398+
<SourceFile path="test1.ts">
399+
<TypeDeclaration name={i1}>any</TypeDeclaration>
400+
<hbr />
401+
<TypeDeclaration name={i2}>any</TypeDeclaration>
402+
</SourceFile>
403+
404+
<SourceFile path="test2.ts">
405+
<FunctionDeclaration
406+
name="foo"
407+
parameters={[{ name: "a", type: i1 }]}
408+
returnType={i2}
409+
/>
410+
</SourceFile>
411+
</Output>
412+
);
413+
414+
expect(template).toRenderTo({
415+
"test1.ts": `
416+
type iface1 = any;
417+
type iface2 = any;
418+
`,
419+
"test2.ts": `
420+
import type { iface1, iface2 } from "./test1.js";
421+
422+
function foo(a: iface1): iface2 {}
423+
`,
424+
});
425+
});

0 commit comments

Comments
 (0)