我想在运行时查看我的.ts文件,并获取有关文件中哪些类,它具有哪些功能,哪些属性以及它们是公共还是私有的信息。
在actionscript3中,我曾经使用“describeType”,它以XML格式返回此信息。 有人知道用打字稿获取这些信息的方法吗?
是否有工具可以解析任何文件类型的jsdoc并返回有关该文件类型的信息?
答案 0 :(得分:2)
由于你想处理文件,我不理解“在运行时”的要求。
除此之外,如果您只想使用TS分析TS代码,您可以使用TypeScript语言服务,它们具有支持TypeScript IDE所需的所有API,所有这些都是用TypeScript编写的。在这个特定的例子中,languageService.getScriptLexicalStructure(file)
应该可以解决问题。
如果您想从命令行执行此操作,或想查看使用语言服务的示例,可以尝试https://github.com/clausreinke/typescript-tools
$ cat reflection.ts
module foo{
export class A{
public x1:number = 10;
public x2 = 20;
public s1: string = "";
constructor(public x = true) {}
}
}
function listProperties(o, isClass?) {
var props = [];
console.log("name:",o["name"]);
var tmp = isClass ? new o : o;
for (var p in tmp)
console.log(p,typeof tmp[p]);
}
listProperties(foo.A,true);
$ node typescript-tools/bin/tss.js reflection.ts
"loaded c:/javascript/typescript/reflection.ts, TSS listening.."
prettyJSON true
"pretty JSON: true"
> structure reflection.ts
[
{
"loc": {
"name": "foo",
"kind": "module",
"kindModifiers": "",
"matchKind": "exact",
"fileName": "c:/javascript/typescript/reflection.ts",
"minChar": 0,
"limChar": 183,
"containerName": "",
"containerKind": ""
},
"file": "c:/javascript/typescript/reflection.ts",
"min": {
"line": 1,
"character": 1
},
"lim": {
"line": 9,
"character": 2
}
},
{
"loc": {
"name": "A",
"kind": "class",
"kindModifiers": "export",
"matchKind": "exact",
"fileName": "c:/javascript/typescript/reflection.ts",
"minChar": 17,
"limChar": 180,
"containerName": "foo",
"containerKind": "module"
},
"file": "c:/javascript/typescript/reflection.ts",
"min": {
"line": 2,
"character": 5
},
"lim": {
"line": 8,
"character": 6
}
},
{
"loc": {
"name": "x1",
"kind": "property",
"kindModifiers": "public",
"matchKind": "exact",
"fileName": "c:/javascript/typescript/reflection.ts",
"minChar": 49,
"limChar": 63,
"containerName": "foo.A",
"containerKind": "class"
},
"file": "c:/javascript/typescript/reflection.ts",
"min": {
"line": 3,
"character": 16
},
"lim": {
"line": 3,
"character": 30
}
},
{
"loc": {
"name": "x2",
"kind": "property",
"kindModifiers": "public",
"matchKind": "exact",
"fileName": "c:/javascript/typescript/reflection.ts",
"minChar": 81,
"limChar": 88,
"containerName": "foo.A",
"containerKind": "class"
},
"file": "c:/javascript/typescript/reflection.ts",
"min": {
"line": 4,
"character": 16
},
"lim": {
"line": 4,
"character": 23
}
},
{
"loc": {
"name": "s1",
"kind": "property",
"kindModifiers": "public",
"matchKind": "exact",
"fileName": "c:/javascript/typescript/reflection.ts",
"minChar": 116,
"limChar": 131,
"containerName": "foo.A",
"containerKind": "class"
},
"file": "c:/javascript/typescript/reflection.ts",
"min": {
"line": 6,
"character": 16
},
"lim": {
"line": 6,
"character": 31
}
},
{
"loc": {
"name": "constructor",
"kind": "constructor",
"kindModifiers": "",
"matchKind": "exact",
"fileName": "c:/javascript/typescript/reflection.ts",
"minChar": 142,
"limChar": 173,
"containerName": "foo.A",
"containerKind": "class"
},
"file": "c:/javascript/typescript/reflection.ts",
"min": {
"line": 7,
"character": 9
},
"lim": {
"line": 7,
"character": 40
}
},
{
"loc": {
"name": "listProperties",
"kind": "function",
"kindModifiers": "",
"matchKind": "exact",
"fileName": "c:/javascript/typescript/reflection.ts",
"minChar": 187,
"limChar": 368,
"containerName": "",
"containerKind": ""
},
"file": "c:/javascript/typescript/reflection.ts",
"min": {
"line": 11,
"character": 1
},
"lim": {
"line": 17,
"character": 2
}
}
]
答案 1 :(得分:1)
没有TypeScript反射API,但您可以使用ES3 JavaScript API! 你应该看看这些功能: