Skip to content

Commit eabc587

Browse files
committed
Do not flatten unions with named members
1 parent a7a64ca commit eabc587

File tree

6 files changed

+161
-38
lines changed

6 files changed

+161
-38
lines changed

src/models/types/compoundTypeUtils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ export function flattenTypes(
1111
nodeToProcess: typeof UnionNode | typeof IntersectionNode,
1212
): TypeNode[] {
1313
let flatTypes: TypeNode[] = [];
14-
nodes.forEach((x) => {
15-
if (x instanceof nodeToProcess) {
16-
flatTypes = flatTypes.concat(flattenTypes(x.types, nodeToProcess));
14+
nodes.forEach((node) => {
15+
if (node instanceof nodeToProcess && !node.name) {
16+
flatTypes = flatTypes.concat(flattenTypes(node.types, nodeToProcess));
1717
} else {
18-
flatTypes.push(x);
18+
flatTypes.push(node);
1919
}
2020
});
2121

src/parsers/typeResolver.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,14 @@ export function resolveType(
9494
typeName = undefined;
9595
}
9696

97-
for (const memberType of type.types) {
98-
memberTypes.push(resolveType(memberType, memberType.getSymbol()?.name || '', context));
97+
// @ts-expect-error - Internal API
98+
if (type.origin) {
99+
// @ts-expect-error - Internal API
100+
return resolveType(type.origin, name, context);
101+
} else {
102+
for (const memberType of type.types) {
103+
memberTypes.push(resolveType(memberType, memberType.getSymbol()?.name || '', context));
104+
}
99105
}
100106

101107
return memberTypes.length === 1 ? memberTypes[0] : new UnionNode(typeName, memberTypes);

test/base-ui-component/output.json

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,7 @@
162162
"types": [
163163
{
164164
"kind": "reference",
165-
"name": "RefCallback"
166-
},
167-
{
168-
"kind": "reference",
169-
"name": "RefObject<any>"
170-
},
171-
{
172-
"kind": "intrinsic",
173-
"name": "null"
165+
"name": "Ref<any>"
174166
},
175167
{
176168
"kind": "intrinsic",
@@ -191,15 +183,7 @@
191183
"types": [
192184
{
193185
"kind": "reference",
194-
"name": "RefCallback"
195-
},
196-
{
197-
"kind": "reference",
198-
"name": "RefObject<any>"
199-
},
200-
{
201-
"kind": "intrinsic",
202-
"name": "null"
186+
"name": "Ref<any>"
203187
},
204188
{
205189
"kind": "intrinsic",

test/literal-unions/input.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@ export function test1(parameters: Params) {}
22
export function test2(
33
inlineStringUnion: 'foo' | 'bar' | 'baz',
44
inlineNumberUnion: 1 | 2 | 3,
5-
referencedStringUnion: StringUniuon,
5+
referencedStringUnion: StringUnion,
66
referencedNumberUnion: NumberUnion,
7+
unionOfUnions: StringUnion | NumberUnion,
78
) {}
89

910
export interface Params {
1011
inlineStringUnion: 'foo' | 'bar' | 'baz';
1112
inlineNumberUnion: 1 | 2 | 3;
12-
referencedStringUnion: StringUniuon;
13+
referencedStringUnion: StringUnion;
1314
referencedNumberUnion: NumberUnion;
15+
callback: (ref: StringUnion | undefined) => void;
16+
unionOfUnions: StringUnion | NumberUnion;
1417
}
1518

16-
type StringUniuon = 'foo' | 'bar' | 'baz';
19+
type StringUnion = 'foo' | 'bar' | 'baz';
1720
type NumberUnion = 1 | 2 | 3;

test/literal-unions/output.json

Lines changed: 140 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"name": "referencedStringUnion",
6363
"type": {
6464
"kind": "union",
65-
"name": "StringUniuon",
65+
"name": "StringUnion",
6666
"types": [
6767
{
6868
"kind": "literal",
@@ -101,6 +101,99 @@
101101
]
102102
},
103103
"optional": false
104+
},
105+
{
106+
"name": "callback",
107+
"type": {
108+
"kind": "function",
109+
"callSignatures": [
110+
{
111+
"parameters": [
112+
{
113+
"nodeType": "parameter",
114+
"name": "ref",
115+
"type": {
116+
"kind": "union",
117+
"types": [
118+
{
119+
"kind": "union",
120+
"name": "StringUnion",
121+
"types": [
122+
{
123+
"kind": "literal",
124+
"value": "\"foo\""
125+
},
126+
{
127+
"kind": "literal",
128+
"value": "\"bar\""
129+
},
130+
{
131+
"kind": "literal",
132+
"value": "\"baz\""
133+
}
134+
]
135+
},
136+
{
137+
"kind": "intrinsic",
138+
"name": "undefined"
139+
}
140+
]
141+
}
142+
}
143+
],
144+
"returnValueType": {
145+
"kind": "intrinsic",
146+
"name": "void"
147+
}
148+
}
149+
]
150+
},
151+
"optional": false
152+
},
153+
{
154+
"name": "unionOfUnions",
155+
"type": {
156+
"kind": "union",
157+
"types": [
158+
{
159+
"kind": "union",
160+
"name": "StringUnion",
161+
"types": [
162+
{
163+
"kind": "literal",
164+
"value": "\"foo\""
165+
},
166+
{
167+
"kind": "literal",
168+
"value": "\"bar\""
169+
},
170+
{
171+
"kind": "literal",
172+
"value": "\"baz\""
173+
}
174+
]
175+
},
176+
{
177+
"kind": "union",
178+
"name": "NumberUnion",
179+
"types": [
180+
{
181+
"kind": "literal",
182+
"value": 1
183+
},
184+
{
185+
"kind": "literal",
186+
"value": 2
187+
},
188+
{
189+
"kind": "literal",
190+
"value": 3
191+
}
192+
]
193+
}
194+
]
195+
},
196+
"optional": false
104197
}
105198
]
106199
}
@@ -169,7 +262,7 @@
169262
"name": "referencedStringUnion",
170263
"type": {
171264
"kind": "union",
172-
"name": "StringUniuon",
265+
"name": "StringUnion",
173266
"types": [
174267
{
175268
"kind": "literal",
@@ -207,6 +300,51 @@
207300
}
208301
]
209302
}
303+
},
304+
{
305+
"nodeType": "parameter",
306+
"name": "unionOfUnions",
307+
"type": {
308+
"kind": "union",
309+
"types": [
310+
{
311+
"kind": "union",
312+
"name": "StringUnion",
313+
"types": [
314+
{
315+
"kind": "literal",
316+
"value": "\"foo\""
317+
},
318+
{
319+
"kind": "literal",
320+
"value": "\"bar\""
321+
},
322+
{
323+
"kind": "literal",
324+
"value": "\"baz\""
325+
}
326+
]
327+
},
328+
{
329+
"kind": "union",
330+
"name": "NumberUnion",
331+
"types": [
332+
{
333+
"kind": "literal",
334+
"value": 1
335+
},
336+
{
337+
"kind": "literal",
338+
"value": 2
339+
},
340+
{
341+
"kind": "literal",
342+
"value": 3
343+
}
344+
]
345+
}
346+
]
347+
}
210348
}
211349
],
212350
"returnValueType": {

test/react-refs/output.json

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,7 @@
7272
"types": [
7373
{
7474
"kind": "reference",
75-
"name": "RefCallback"
76-
},
77-
{
78-
"kind": "reference",
79-
"name": "RefObject<HTMLInputElement | null>"
80-
},
81-
{
82-
"kind": "intrinsic",
83-
"name": "null"
75+
"name": "Ref<HTMLInputElement>"
8476
},
8577
{
8678
"kind": "intrinsic",

0 commit comments

Comments
 (0)