3
私は、JSONはこのようなものになる可能性があり、このようなtypescript定義をjsonに解析するには?
namespace call.common {
namespace login {
interface Request {
userName: string;
password: string;
}
interface Response {
isLogin: boolean
}
}
}
として、JSONに私のtypescriptです定義を解析したい:私はコンパイラを使用しようとしています文書Using-the-Compiler-API
による
{
namespace: "call.common.login",
interfaces: [
{
"name": "Request",
params: [{
name: "userName",
type: "string",
}, {
name: "password",
type: "string"
}]
}, {
"name": "Response",
params: [{
name: "isLogin",
type: boolean
}]
}
]
}
:
function parseGrpcTypings(fileName, options) {
var program = ts.createProgram(fileName, options);
var checker = program.getTypeChecker();
const sourceFile = program.getSourceFiles()[0];
ts.forEachChild(sourceFile, visit);
function visit(node) {
if (!isNodeExported(node)) {
return;
}
if (node.kind === ts.SyntaxKind.ModuleDeclaration) {
ts.forEachChild(node, visit);
} else if (node.kind === ts.SyntaxKind.InterfaceDeclaration) {
// how to parse interface
} else {
// how to deal with namespace
}
}
function isNodeExported(node) {
return (node.flags & ts.NodeFlags.Export) !== 0 || (node.parent && node.parent.kind === ts.SyntaxKind.SourceFile);
}
}
parseGrpcTypings("/tmp/test.d.ts", {
target: ts.ScriptTarget.ES5, module: ts.ModuleKind.CommonJS
});
をそして、どのようにあなたは、このJSONが見えるようになりよう:出力
?どのようにそれを解析したいですか?もっと情報が役に立つかもしれません... –
@NitzanTomer私が投稿したようなものですが、それは本当に簡単な例です – roger