Skip to content

Commit 15299ab

Browse files
committed
TS: Workaround issue with infer types
1 parent 5c8dd7e commit 15299ab

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

javascript/extractor/lib/typescript/src/ast_extractor.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,21 @@ export function augmentAst(ast: AugmentedSourceFile, code: string, project: Proj
156156
}
157157
}
158158

159-
forEachNode(ast, (node: AugmentedNode) => {
159+
// Number of conditional type expressions the visitor is currently inside.
160+
// We disable type extraction inside such type expressions, to avoid complications
161+
// with `infer` types.
162+
let insideConditionalTypes = 0;
163+
164+
visitAstNode(ast);
165+
function visitAstNode(node: AugmentedNode) {
166+
if (node.kind === ts.SyntaxKind.ConditionalType) {
167+
++insideConditionalTypes;
168+
}
169+
ts.forEachChild(node, visitAstNode);
170+
if (node.kind === ts.SyntaxKind.ConditionalType) {
171+
--insideConditionalTypes;
172+
}
173+
160174
// fill in line/column info
161175
if ("pos" in node) {
162176
node.$pos = augmentPos(node.pos, true);
@@ -176,7 +190,7 @@ export function augmentAst(ast: AugmentedSourceFile, code: string, project: Proj
176190
}
177191
}
178192

179-
if (typeChecker != null) {
193+
if (typeChecker != null && insideConditionalTypes === 0) {
180194
if (isTypedNode(node)) {
181195
let contextualType = isContextuallyTypedNode(node)
182196
? typeChecker.getContextualType(node)
@@ -250,7 +264,7 @@ export function augmentAst(ast: AugmentedSourceFile, code: string, project: Proj
250264
}
251265
}
252266
}
253-
});
267+
}
254268
}
255269

256270
type NamedNodeWithSymbol = AugmentedNode & (ts.ClassDeclaration | ts.InterfaceDeclaration

0 commit comments

Comments
 (0)