From 0d99e7e1e3dab22838087596fb06d1924c404d0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dudak?= Date: Mon, 26 May 2025 14:31:15 +0200 Subject: [PATCH 1/3] Treat conditional types as unions --- src/parsers/typeResolver.ts | 19 +++--- test/conditional-types/input.ts | 11 +++ test/conditional-types/output.json | 104 +++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 11 deletions(-) create mode 100644 test/conditional-types/input.ts create mode 100644 test/conditional-types/output.json diff --git a/src/parsers/typeResolver.ts b/src/parsers/typeResolver.ts index 21f32c1..1245dd2 100644 --- a/src/parsers/typeResolver.ts +++ b/src/parsers/typeResolver.ts @@ -238,17 +238,14 @@ 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, - ); - return resolveType(trueType, name, context); - } + return new UnionNode( + undefined, + [], + [ + resolveType((type as ts.ConditionalType).resolvedTrueType!, '', context), + resolveType((type as ts.ConditionalType).resolvedFalseType!, '', context), + ], + ); } console.warn( 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 From db031adf6c114dc6d847a96b224eacbc7ff3113a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dudak?= Date: Mon, 26 May 2025 14:39:49 +0200 Subject: [PATCH 2/3] Check if both branches exist --- src/parsers/typeResolver.ts | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/parsers/typeResolver.ts b/src/parsers/typeResolver.ts index 1245dd2..15036a5 100644 --- a/src/parsers/typeResolver.ts +++ b/src/parsers/typeResolver.ts @@ -238,14 +238,21 @@ export function resolveType( } if (type.flags & ts.TypeFlags.Conditional) { - return new UnionNode( - undefined, - [], - [ - resolveType((type as ts.ConditionalType).resolvedTrueType!, '', context), - resolveType((type as ts.ConditionalType).resolvedFalseType!, '', context), - ], - ); + 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), + ], + ); + } else if (conditionalType.resolvedTrueType) { + return resolveType(conditionalType.resolvedTrueType, '', context); + } else if (conditionalType.resolvedFalseType) { + return resolveType(conditionalType.resolvedFalseType, '', context); + } } console.warn( From 980a68754cd4acc56a76c00045a2980fb04eb7d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dudak?= Date: Mon, 26 May 2025 14:54:09 +0200 Subject: [PATCH 3/3] Restart CI