Skip to content

Commit 5ed3c50

Browse files
committed
TS: Workaround issue with infer types
1 parent c7300fa commit 5ed3c50

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
@@ -154,7 +154,21 @@ export function augmentAst(ast: AugmentedSourceFile, code: string, project: Proj
154154
}
155155
}
156156

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

177-
if (typeChecker != null) {
191+
if (typeChecker != null && insideConditionalTypes === 0) {
178192
if (isTypedNode(node)) {
179193
let type = typeChecker.getTypeAtLocation(node);
180194
if (type != null) {
@@ -245,7 +259,7 @@ export function augmentAst(ast: AugmentedSourceFile, code: string, project: Proj
245259
}
246260
}
247261
}
248-
});
262+
}
249263
}
250264

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

0 commit comments

Comments
 (0)