import * as ts from 'typescript'; function walkToNodeWithJSDoc(node: ts.Node): (ts.Node & { jsDoc?: ts.Node[] }) | undefined { let relevantNode: (ts.Node & { jsDoc?: ts.Node[] }) | undefined = node; while (relevantNode) { const { jsDoc } = relevantNode; if (jsDoc && jsDoc instanceof Array && jsDoc.length > 0) { return relevantNode; } relevantNode = relevantNode.parent; } return relevantNode; } export function extractDocumentationText(decl: ts.Declaration): string | undefined { const n = walkToNodeWithJSDoc(decl); if (!n) { return undefined; } const { jsDoc }: { jsDoc?: ts.Node[] } = n; if (jsDoc && jsDoc.length > 0) { const commentText: string = jsDoc[0].getText(); return commentText; } return undefined; }