Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/parsers/typeResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,16 +238,20 @@ export function resolveType(
}

if (type.flags & ts.TypeFlags.Conditional) {
// We don't fully support conditional types. We assume the condition is always true.
if (
type.aliasSymbol?.declarations?.[0] &&
ts.isTypeAliasDeclaration(type.aliasSymbol.declarations[0]) &&
ts.isConditionalTypeNode(type.aliasSymbol.declarations[0].type)
) {
const trueType = checker.getTypeFromTypeNode(
type.aliasSymbol.declarations[0].type.trueType,
const conditionalType = type as ts.ConditionalType;
if (conditionalType.resolvedTrueType && conditionalType.resolvedFalseType) {
return new UnionNode(
undefined,
[],
[
resolveType((type as ts.ConditionalType).resolvedTrueType!, '', context),
resolveType((type as ts.ConditionalType).resolvedFalseType!, '', context),
],
);
return resolveType(trueType, name, context);
} else if (conditionalType.resolvedTrueType) {
return resolveType(conditionalType.resolvedTrueType, '', context);
} else if (conditionalType.resolvedFalseType) {
return resolveType(conditionalType.resolvedFalseType, '', context);
}
}

Expand Down
11 changes: 11 additions & 0 deletions test/conditional-types/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function fn<T extends boolean>(x: T): T extends true ? number : null {
return (x ? 1 : null) as T extends true ? number : null;
}

export function Component1<Multiple extends boolean>(props: Props<Multiple>) {
return null;
}

interface Props<Multiple extends boolean> {
value: Multiple extends true ? string | null : string[];
}
104 changes: 104 additions & 0 deletions test/conditional-types/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{
"name": "test/conditional-types/input",
"exports": [
{
"name": "fn",
"type": {
"kind": "function",
"name": "fn",
"parentNamespaces": [],
"callSignatures": [
{
"parameters": [
{
"type": {
"kind": "typeParameter",
"name": "T",
"parentNamespaces": [],
"constraint": "boolean"
},
"name": "x",
"optional": false
}
],
"returnValueType": {
"kind": "union",
"types": [
{
"kind": "intrinsic",
"parentNamespaces": [],
"name": "number"
},
{
"kind": "intrinsic",
"parentNamespaces": [],
"name": "null"
}
],
"parentNamespaces": []
}
}
]
}
},
{
"name": "Component1",
"type": {
"kind": "function",
"name": "Component1",
"parentNamespaces": [],
"callSignatures": [
{
"parameters": [
{
"type": {
"kind": "object",
"name": "Props",
"parentNamespaces": [],
"properties": [
{
"name": "value",
"type": {
"kind": "union",
"types": [
{
"kind": "intrinsic",
"parentNamespaces": [],
"name": "string"
},
{
"kind": "array",
"parentNamespaces": [],
"elementType": {
"kind": "intrinsic",
"parentNamespaces": [],
"name": "string"
}
},
{
"kind": "intrinsic",
"parentNamespaces": [],
"name": "null"
}
],
"parentNamespaces": []
},
"optional": false
}
]
},
"name": "props",
"optional": false
}
],
"returnValueType": {
"kind": "intrinsic",
"parentNamespaces": [],
"name": "null"
}
}
]
}
}
]
}