简体   繁体   English

如何在打字稿中提取函数的类型?

[英]How to extract the type of a function in typescript?

Say I have this file foo.ts : 说我有这个文件foo.ts

export const render = function ( a : Image, b : number ) : Image {
  // ...
}

How can I extract the type of "render" ? 如何提取“渲染”的类型?

I would like to end up with JSON like this: 我想以这样的JSON结尾:

{ "render": { "params": [ "Image", "number" ], "return": "Image" } }

Use case : I am building a tool where the user can visually connect elements of typescript code and I would like to cache the types of the "slots" so that I can show visually which connections are possible in advance. 用例 :我正在构建一个工具,用户可以在其中直观地键入打字稿代码的元素,并且希望缓存“插槽”的类型,以便可以提前直观地显示哪些连接是可能的。

There's no way to do that in typescript (as I'm aware). (我知道)没有办法在打字稿中做到这一点。

What you run is javascript not typescript and all of the types aren't available in the compiled javascript code. 您运行的是javascript而非打字稿,并且所有类型在已编译的javascript代码中均不可用。

If you have the code as typescript, you can use the typescript compiler to build a syntax tree and then from that get what you want. 如果您将代码作为打字稿,则可以使用打字稿编译器构建语法树,然后从中获得所需的内容。
You can do that with node, probably using the typescript module itself but it might not be easy nor well documented, there's this typescript-compiler project ( npm | github ) but doesn't look like it's being maintained. 您可以使用node来做到这一点,可能使用typescript模块本身,但是它可能并不容易,也没有很好的文档记录,这里有一个typescript-compiler项目( npm | github ),但看起来好像没有得到维护。

However, if you want to do that in the client alone then you might have a problem. 但是,如果只想在客户端中执行此操作,则可能会遇到问题。
A short search popped out typescript-script and typescript-compile , but I haven't really looked into what those do and how, so I'm not sure if they can serve your purposes. 简短的搜索弹出了typescript-scripttypescript-compile ,但是我还没有真正研究它们的作用和方式,因此我不确定它们是否可以满足您的目的。


Edit 编辑

Here's another thought, you might save yourself a lot of trouble if you require the user to provide the .d.ts files along with the .ts files. 这是另一种想法,如果要求用户提供.d.ts文件和.ts文件,则可能会省去很多麻烦。
From that you can get the needed info, you'll have to start searching and comparing between the files, but that's different kind of trouble. 由此,您可以获得所需的信息,您必须开始在文件之间进行搜索和比较,但这是另一种麻烦。

I am building a tool where the user can visually connect elements of typescript code and I would like to cache the types of the "slots" so that I can show visually which connections are possible in advance. 我正在构建一个工具,用户可以在其中可视地连接打字稿代码的元素,并且希望缓存“插槽”的类型,以便可以提前直观地显示哪些连接是可能的。

You need to get a hold of the typechecker ( what is it ). 你需要得到的保持typechecker它是什么 )。

As a sample you can get display parts using a method exported by the npm typescript module : 作为示例,您可以使用npm typescript模块导出的方法来获取显示部件

https://github.com/TypeStrong/atom-typescript/blob/ae66fbbba503aaa2329209421f3362a35f89127c/lib/main/lang/fixmyts/quickFixes/extractVariable.ts#L179-L180 https://github.com/TypeStrong/atom-typescript/blob/ae66fbbba503aaa2329209421f3362a35f89127c/lib/main/lang/fixmyts/quickFixes/extractVariable.ts#L179-L180

let type = typeChecker.getTypeAtLocation(node);
let typeSignature = ts.displayPartsToString(ts.typeToDisplayParts(typeChecker, type)).replace(/\s+/g, ' ');

That said I am writing a tool that can help other tool developers like you : http://alm.tools/ 也就是说,我正在编写一种可以帮助像您这样的其他工具开发人员的工具: http : //alm.tools/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM