Skip to content

Commit 86cf283

Browse files
committed
Resolve simple aliases in union members
1 parent cb90379 commit 86cf283

File tree

5 files changed

+135
-150
lines changed

5 files changed

+135
-150
lines changed

src/parsers/unionTypeResolver.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ export function resolveUnionType(
3030
memberTypes = type.origin.types;
3131
}
3232

33+
const typeAliasDeclaration = type.aliasSymbol?.declarations?.[0];
34+
35+
if (
36+
typeAliasDeclaration &&
37+
ts.isTypeAliasDeclaration(typeAliasDeclaration) &&
38+
ts.isUnionTypeNode(typeAliasDeclaration.type)
39+
) {
40+
typeNode = typeAliasDeclaration.type;
41+
}
42+
3343
if (typeNode && ts.isUnionTypeNode(typeNode)) {
3444
// Here we're trying to match the union member types with TypeNodes
3545
// (what TS resolves to what was authored in code).

test/aliases-in-unions/input.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export function f(x: Params) {}
2+
3+
type Params = Alias | 0;
4+
5+
type SomeType = 1 | 2;
6+
type Alias = SomeType;

test/aliases-in-unions/output.json

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"name": "test/aliases-in-unions/input",
3+
"exports": [
4+
{
5+
"name": "f",
6+
"type": {
7+
"kind": "function",
8+
"typeName": {
9+
"name": "f"
10+
},
11+
"callSignatures": [
12+
{
13+
"parameters": [
14+
{
15+
"type": {
16+
"kind": "union",
17+
"typeName": {
18+
"name": "Params"
19+
},
20+
"types": [
21+
{
22+
"kind": "literal",
23+
"value": 0
24+
},
25+
{
26+
"kind": "union",
27+
"typeName": {
28+
"name": "Alias"
29+
},
30+
"types": [
31+
{
32+
"kind": "literal",
33+
"value": 1
34+
},
35+
{
36+
"kind": "literal",
37+
"value": 2
38+
}
39+
]
40+
}
41+
]
42+
},
43+
"name": "x",
44+
"optional": false
45+
}
46+
],
47+
"returnValueType": {
48+
"kind": "intrinsic",
49+
"intrinsic": "void"
50+
}
51+
}
52+
]
53+
}
54+
}
55+
]
56+
}

test/mapped-types-custom/input.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
export function Comp(props: Props) {}
22

33
export interface Props {
4-
coords?: Coords;
5-
rect?: Rect;
4+
r: Rect;
65
}
76

87
// types from Floating UI Utils:

test/mapped-types-custom/output.json

Lines changed: 62 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -19,91 +19,48 @@
1919
},
2020
"properties": [
2121
{
22-
"name": "coords",
22+
"name": "r",
2323
"type": {
24-
"kind": "union",
25-
"types": [
24+
"kind": "object",
25+
"typeName": {
26+
"name": "Rect"
27+
},
28+
"properties": [
2629
{
27-
"kind": "object",
28-
"typeName": {
29-
"name": "Coords"
30+
"name": "x",
31+
"type": {
32+
"kind": "intrinsic",
33+
"intrinsic": "number"
3034
},
31-
"properties": [
32-
{
33-
"name": "x",
34-
"type": {
35-
"kind": "intrinsic",
36-
"intrinsic": "number"
37-
},
38-
"optional": false
39-
},
40-
{
41-
"name": "y",
42-
"type": {
43-
"kind": "intrinsic",
44-
"intrinsic": "number"
45-
},
46-
"optional": false
47-
}
48-
]
35+
"optional": false
4936
},
5037
{
51-
"kind": "intrinsic",
52-
"intrinsic": "undefined"
53-
}
54-
]
55-
},
56-
"optional": true
57-
},
58-
{
59-
"name": "rect",
60-
"type": {
61-
"kind": "union",
62-
"types": [
38+
"name": "y",
39+
"type": {
40+
"kind": "intrinsic",
41+
"intrinsic": "number"
42+
},
43+
"optional": false
44+
},
6345
{
64-
"kind": "object",
65-
"properties": [
66-
{
67-
"name": "x",
68-
"type": {
69-
"kind": "intrinsic",
70-
"intrinsic": "number"
71-
},
72-
"optional": false
73-
},
74-
{
75-
"name": "y",
76-
"type": {
77-
"kind": "intrinsic",
78-
"intrinsic": "number"
79-
},
80-
"optional": false
81-
},
82-
{
83-
"name": "width",
84-
"type": {
85-
"kind": "intrinsic",
86-
"intrinsic": "number"
87-
},
88-
"optional": false
89-
},
90-
{
91-
"name": "height",
92-
"type": {
93-
"kind": "intrinsic",
94-
"intrinsic": "number"
95-
},
96-
"optional": false
97-
}
98-
]
46+
"name": "width",
47+
"type": {
48+
"kind": "intrinsic",
49+
"intrinsic": "number"
50+
},
51+
"optional": false
9952
},
10053
{
101-
"kind": "intrinsic",
102-
"intrinsic": "undefined"
54+
"name": "height",
55+
"type": {
56+
"kind": "intrinsic",
57+
"intrinsic": "number"
58+
},
59+
"optional": false
10360
}
10461
]
10562
},
106-
"optional": true
63+
"optional": false
10764
}
10865
]
10966
},
@@ -128,91 +85,48 @@
12885
},
12986
"properties": [
13087
{
131-
"name": "coords",
88+
"name": "r",
13289
"type": {
133-
"kind": "union",
134-
"types": [
90+
"kind": "object",
91+
"typeName": {
92+
"name": "Rect"
93+
},
94+
"properties": [
13595
{
136-
"kind": "object",
137-
"typeName": {
138-
"name": "Coords"
96+
"name": "x",
97+
"type": {
98+
"kind": "intrinsic",
99+
"intrinsic": "number"
139100
},
140-
"properties": [
141-
{
142-
"name": "x",
143-
"type": {
144-
"kind": "intrinsic",
145-
"intrinsic": "number"
146-
},
147-
"optional": false
148-
},
149-
{
150-
"name": "y",
151-
"type": {
152-
"kind": "intrinsic",
153-
"intrinsic": "number"
154-
},
155-
"optional": false
156-
}
157-
]
101+
"optional": false
158102
},
159103
{
160-
"kind": "intrinsic",
161-
"intrinsic": "undefined"
162-
}
163-
]
164-
},
165-
"optional": true
166-
},
167-
{
168-
"name": "rect",
169-
"type": {
170-
"kind": "union",
171-
"types": [
104+
"name": "y",
105+
"type": {
106+
"kind": "intrinsic",
107+
"intrinsic": "number"
108+
},
109+
"optional": false
110+
},
172111
{
173-
"kind": "object",
174-
"properties": [
175-
{
176-
"name": "x",
177-
"type": {
178-
"kind": "intrinsic",
179-
"intrinsic": "number"
180-
},
181-
"optional": false
182-
},
183-
{
184-
"name": "y",
185-
"type": {
186-
"kind": "intrinsic",
187-
"intrinsic": "number"
188-
},
189-
"optional": false
190-
},
191-
{
192-
"name": "width",
193-
"type": {
194-
"kind": "intrinsic",
195-
"intrinsic": "number"
196-
},
197-
"optional": false
198-
},
199-
{
200-
"name": "height",
201-
"type": {
202-
"kind": "intrinsic",
203-
"intrinsic": "number"
204-
},
205-
"optional": false
206-
}
207-
]
112+
"name": "width",
113+
"type": {
114+
"kind": "intrinsic",
115+
"intrinsic": "number"
116+
},
117+
"optional": false
208118
},
209119
{
210-
"kind": "intrinsic",
211-
"intrinsic": "undefined"
120+
"name": "height",
121+
"type": {
122+
"kind": "intrinsic",
123+
"intrinsic": "number"
124+
},
125+
"optional": false
212126
}
213127
]
214128
},
215-
"optional": true
129+
"optional": false
216130
}
217131
]
218132
}

0 commit comments

Comments
 (0)