Skip to content

Commit bae3272

Browse files
committed
Comments + tests
1 parent 86cf283 commit bae3272

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

src/parsers/unionTypeResolver.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,19 @@ export function resolveUnionType(
3030
memberTypes = type.origin.types;
3131
}
3232

33+
// If there's no provided type node or it's is not a union,
34+
// We check if the type declaration is an alias.
35+
// If so, it can point to the original union type.
36+
//
37+
// For example:
38+
// function f(x: Params) {}
39+
// type Params = SomeType | SomeOtherType;
40+
//
41+
// In this case `typeNode` will be set to the type reference of the function parameter,
42+
// so we extract the needed union definition.
3343
const typeAliasDeclaration = type.aliasSymbol?.declarations?.[0];
34-
3544
if (
45+
(!typeNode || !ts.isUnionTypeNode(typeNode)) &&
3646
typeAliasDeclaration &&
3747
ts.isTypeAliasDeclaration(typeAliasDeclaration) &&
3848
ts.isUnionTypeNode(typeAliasDeclaration.type)

test/aliases-in-unions/input.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
export function f(x: Params) {}
22

3+
export interface Props {
4+
x: Params;
5+
}
6+
37
type Params = Alias | 0;
48

59
type SomeType = 1 | 2;

test/aliases-in-unions/output.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,49 @@
5151
}
5252
]
5353
}
54+
},
55+
{
56+
"name": "Props",
57+
"type": {
58+
"kind": "object",
59+
"typeName": {
60+
"name": "Props"
61+
},
62+
"properties": [
63+
{
64+
"name": "x",
65+
"type": {
66+
"kind": "union",
67+
"typeName": {
68+
"name": "Params"
69+
},
70+
"types": [
71+
{
72+
"kind": "literal",
73+
"value": 0
74+
},
75+
{
76+
"kind": "union",
77+
"typeName": {
78+
"name": "Alias"
79+
},
80+
"types": [
81+
{
82+
"kind": "literal",
83+
"value": 1
84+
},
85+
{
86+
"kind": "literal",
87+
"value": 2
88+
}
89+
]
90+
}
91+
]
92+
},
93+
"optional": false
94+
}
95+
]
96+
}
5497
}
5598
]
5699
}

0 commit comments

Comments
 (0)