Is there a way to dynamically create a class using Typescript which can be saved in it's own file to be reused later on in different projects?
例如,假设我想要一个 SpecificationParser
类,它可以读取一些 JSON
规范文件并创建某个实体的表示 .
我希望动态创建一个类,它将采用表示形式并将其重新化为构建器类,该类将具有动态创建的方法,这些方法具有来自相同表示的属性(命名,类型,参数...) .
spec: {
a: {
type: string,
length: 10
},
b: {
type: number,
min: 3,
max: 9
}
}
+
|
|
|
+--------v----------+ +-------------------+ +--------------------+
|SpecificationParser| |Representation | |DynamicClass |
+-------------------+ +-------------------+ +--------------------+
|Parse +----->Compile +----->withA(value: string)|
| | | | | |
| | | | |withB(value: number)|
+-------------------+ +-------------------+ +--------------------+
The things I'd like to achieve:
-
从规范文件动态创建TS类
-
这些类受益于TS功能(类型检查......)
是否有来自TS环境的工具可以实现这一目标?
如果没有,可以/应该使用哪些工具?