Trying to use this lib in a TypeScript project of my own and having difficulty taking advantage of the typings. For example, the following code works (it's just something i'm hacking at right now):
import Ast from "ts-simple-ast";
// import { NamespaceDeclaration } from "ts-simple-ast";
import * as ts from "typescript";
// import * as _ from "lodash";
const ast = new Ast({
tsConfigFilePath: "tsconfig.json"
});
ast.addSourceFiles("msgs/**/*{.d.ts,.ts}");
ast.getSourceFiles().forEach((sf, idx) => {
if (idx === 0) {
console.log(sf.getFilePath());
sf.getNamespaces().forEach(ns => processNS([], ns));
console.log("=================");
}
});
function processNS(nsParents, ns) {
let currNSStr = nsParents.length === 0 ? ns.node.name.text : nsParents.map(ns => ns.node.name.text).join(".");
ns.getClasses().forEach(c => {
console.log(`${currNSStr}.${c.node.name.text}`);
});
let currNS = nsParents.concat(ns);
ns.getChildrenOfKind(ts.SyntaxKind.ModuleDeclaration).forEach(childNS => {
processNS(currNS, childNS);
});
}
The problem is I'd like, for example, for the function to be written as follows:
function processNS(nsParents, ns: NamespaceDeclaration) {
let currNSStr = nsParents.length === 0 ? ns.node.name.text : nsParents.map(ns => ns.node.name.text).join(".");
ns.getClasses().forEach(c => {
console.log(`${currNSStr}.${c.node.name.text}`);
});
let currNS = nsParents.concat(ns);
ns.getChildrenOfKind(ts.SyntaxKind.ModuleDeclaration).forEach(childNS => {
processNS(currNS, childNS);
});
}
But then vscode complains about the "node" portion of "ns.node.name.text" and "c.node.name.text" (and it also complains about "childNS" which it says is of type Node<ts.Node>).
If I change it instead to:
function processNS(nsParents, ns: ts.NamespaceDeclaration) {
let currNSStr = nsParents.length === 0 ? ns.node.name.text : nsParents.map(ns => ns.node.name.text).join(".");
ns.getClasses().forEach(c => {
console.log(`${currNSStr}.${c.node.name.text}`);
});
let currNS = nsParents.concat(ns);
ns.getChildrenOfKind(ts.SyntaxKind.ModuleDeclaration).forEach(childNS => {
processNS(currNS, childNS);
});
}
Then it also complains about the "node" in "ns.node.name.text" but also "getClasses" of "ns.getClasses()" and "getChildrenOfKind" of "ns.getChildrenOfKind().
So, the question is - how should I be setting that type such that I can take advantage of the typing system?