Skip to content

Commit c5ed930

Browse files
committed
Handle exported interfaces
1 parent a900286 commit c5ed930

File tree

6 files changed

+185
-3
lines changed

6 files changed

+185
-3
lines changed

src/parsers/exportParser.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ export function parseExport(
5959
return;
6060
}
6161

62-
const type = checker.getTypeOfSymbol(targetSymbol);
62+
let type: ts.Type;
63+
if (targetSymbol.declarations?.length) {
64+
type = checker.getTypeAtLocation(targetSymbol.declarations[0]);
65+
} else {
66+
type = checker.getTypeOfSymbol(targetSymbol);
67+
}
6368
return createExportNode(exportSymbol.name, targetSymbol, type, parentNamespaces);
6469
} else if (ts.isExportAssignment(exportDeclaration)) {
6570
// export default x
@@ -96,6 +101,19 @@ export function parseExport(
96101

97102
const type = checker.getTypeOfSymbol(exportedSymbol);
98103
return createExportNode(exportSymbol.name, exportedSymbol, type, parentNamespaces);
104+
} else if (ts.isInterfaceDeclaration(exportDeclaration)) {
105+
// export interface X {}
106+
if (!exportDeclaration.name) {
107+
return;
108+
}
109+
110+
const exportedSymbol = checker.getSymbolAtLocation(exportDeclaration.name);
111+
if (!exportedSymbol) {
112+
return;
113+
}
114+
115+
const type = checker.getTypeAtLocation(exportDeclaration);
116+
return createExportNode(exportSymbol.name, exportedSymbol, type, parentNamespaces);
99117
} else if (ts.isEnumDeclaration(exportDeclaration)) {
100118
// export enum x {}
101119
if (!exportDeclaration.name) {

test/generics/input.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export function genericFunction<T extends React.HTMLAttributes<HTMLElement>>(
44
return params.value;
55
}
66

7-
export interface GenericFunctionParameters<T extends React.HTMLAttributes<HTMLElement>> {
7+
interface GenericFunctionParameters<T extends React.HTMLAttributes<HTMLElement>> {
88
value: T;
99
nestedGenericType: GenericInterface<GenericInterface<string>>;
1010
}

test/interfaces/input.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export interface Interface1 {
2+
s: string;
3+
}
4+
5+
interface Interface2 {
6+
n: number;
7+
}
8+
9+
export type { Interface2 };
10+
export type { Interface2 as Interface2Alias };
11+
12+
export interface Interface1 {
13+
b: boolean;
14+
}
15+
16+
export default interface DefaultInterface {
17+
n1: number;
18+
n2?: number;
19+
}

test/interfaces/output.json

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
{
2+
"name": "test/interfaces/input",
3+
"exports": [
4+
{
5+
"name": "Interface1",
6+
"type": {
7+
"kind": "object",
8+
"name": "Interface1",
9+
"parentNamespaces": [],
10+
"properties": [
11+
{
12+
"name": "s",
13+
"type": {
14+
"kind": "intrinsic",
15+
"parentNamespaces": [],
16+
"name": "string"
17+
},
18+
"optional": false
19+
},
20+
{
21+
"name": "b",
22+
"type": {
23+
"kind": "intrinsic",
24+
"parentNamespaces": [],
25+
"name": "boolean"
26+
},
27+
"optional": false
28+
}
29+
]
30+
}
31+
},
32+
{
33+
"name": "Interface2",
34+
"type": {
35+
"kind": "object",
36+
"name": "Interface2",
37+
"parentNamespaces": [],
38+
"properties": [
39+
{
40+
"name": "n",
41+
"type": {
42+
"kind": "intrinsic",
43+
"parentNamespaces": [],
44+
"name": "number"
45+
},
46+
"optional": false
47+
}
48+
]
49+
}
50+
},
51+
{
52+
"name": "Interface2Alias",
53+
"type": {
54+
"kind": "object",
55+
"name": "Interface2",
56+
"parentNamespaces": [],
57+
"properties": [
58+
{
59+
"name": "n",
60+
"type": {
61+
"kind": "intrinsic",
62+
"parentNamespaces": [],
63+
"name": "number"
64+
},
65+
"optional": false
66+
}
67+
]
68+
}
69+
},
70+
{
71+
"name": "default",
72+
"type": {
73+
"kind": "object",
74+
"name": "default",
75+
"parentNamespaces": [],
76+
"properties": [
77+
{
78+
"name": "n1",
79+
"type": {
80+
"kind": "intrinsic",
81+
"parentNamespaces": [],
82+
"name": "number"
83+
},
84+
"optional": false
85+
},
86+
{
87+
"name": "n2",
88+
"type": {
89+
"kind": "union",
90+
"types": [
91+
{
92+
"kind": "intrinsic",
93+
"parentNamespaces": [],
94+
"name": "number"
95+
},
96+
{
97+
"kind": "intrinsic",
98+
"parentNamespaces": [],
99+
"name": "undefined"
100+
}
101+
],
102+
"parentNamespaces": []
103+
},
104+
"optional": true
105+
}
106+
]
107+
}
108+
}
109+
]
110+
}

test/literal-unions/input.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function test2(
1111

1212
export function test3(prop: keyof Params) {}
1313

14-
export interface Params {
14+
interface Params {
1515
inlineStringUnion: 'foo' | 'bar' | 'baz';
1616
inlineNumberUnion: 1 | 2 | 3;
1717
referencedStringUnion: StringUnion;

test/namespaces/output.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,41 @@
171171
]
172172
}
173173
},
174+
{
175+
"name": "Params",
176+
"type": {
177+
"kind": "object",
178+
"name": "Params",
179+
"parentNamespaces": [
180+
"Root",
181+
"Sub"
182+
],
183+
"properties": [
184+
{
185+
"name": "s",
186+
"type": {
187+
"kind": "enum",
188+
"name": "Grade",
189+
"parentNamespaces": [
190+
"Root",
191+
"Sub"
192+
],
193+
"members": [
194+
{
195+
"name": "good",
196+
"value": 0
197+
},
198+
{
199+
"name": "bad",
200+
"value": 1
201+
}
202+
]
203+
},
204+
"optional": false
205+
}
206+
]
207+
}
208+
},
174209
{
175210
"name": "Grade",
176211
"type": {

0 commit comments

Comments
 (0)