diff --git a/src/parsers/typeResolver.ts b/src/parsers/typeResolver.ts index 21f32c1..15036a5 100644 --- a/src/parsers/typeResolver.ts +++ b/src/parsers/typeResolver.ts @@ -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); } } diff --git a/test/conditional-types/input.ts b/test/conditional-types/input.ts new file mode 100644 index 0000000..767a5f2 --- /dev/null +++ b/test/conditional-types/input.ts @@ -0,0 +1,11 @@ +export function fn(x: T): T extends true ? number : null { + return (x ? 1 : null) as T extends true ? number : null; +} + +export function Component1(props: Props) { + return null; +} + +interface Props { + value: Multiple extends true ? string | null : string[]; +} diff --git a/test/conditional-types/output.json b/test/conditional-types/output.json new file mode 100644 index 0000000..b04e5b5 --- /dev/null +++ b/test/conditional-types/output.json @@ -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" + } + } + ] + } + } + ] +} \ No newline at end of file