Typescript 如何从文件中获取所有接口名称?(打字稿)
我想从一个特定的文件中获取所有接口的名称 例如: file1.tsTypescript 如何从文件中获取所有接口名称?(打字稿),typescript,nameof,Typescript,Nameof,我想从一个特定的文件中获取所有接口的名称 例如: file1.ts private interface first{ ... } private interface second{ ... } private interface third{ ... } const interfacesList = GetInterfacesFrom(filePath); //in this case filePath = file1.ts file2.ts private interfac
private interface first{
...
}
private interface second{
...
}
private interface third{
...
}
const interfacesList = GetInterfacesFrom(filePath); //in this case filePath = file1.ts
file2.ts
private interface first{
...
}
private interface second{
...
}
private interface third{
...
}
const interfacesList = GetInterfacesFrom(filePath); //in this case filePath = file1.ts
我希望返回:[“第一”、“第二”、“第三”]代码>
更新
我想在typescript中构建一个类似EntityFramework的机制
但是我需要知道如何自动将接口/类的名称与api路径匹配,知道是否有接口或类要调用哪个api…您需要使用编译器api
- 公文
API允许您解析源代码并从中获取符号 您需要使用编译器API
- 公文
API允许您解析源代码并从中获取符号 在常规脚本中不能这样做,因为接口没有被编译成javascript等价物,所以在运行时没有接口。您可能可以使用导出来实现这一点,所以我发现上面建议的代码非常不正统。您可能有一个不明显的要求。如果你告诉我们更多你想做的事情的细节,人们可能会引导你走向正确的方向。正如Nitzan Tomer所说,这些接口并不是最终JS代码的一部分,所以你不能在运行时对它们进行反思。你不能在常规脚本中这样做,因为接口没有被编译成javascript等价物,所以在运行时没有接口。您可能可以使用导出来实现这一点,所以我发现上面建议的代码非常不正统。您可能有一个不明显的要求。如果你告诉我们更多你想做的事情的细节,人们可能会引导你走向正确的方向。正如Nitzan Tomer所说,这些接口并不是最终JS代码的一部分,所以您不能在运行时对它们进行反思。