是否有TypeScript的代码生成API?

时间:2016-04-04 15:43:49

标签: typescript code-generation t4 roslyn

当我需要生成一些C#代码,例如xsd schema中的DTO类或excel表时,我已经使用了一些roslyn API。

打字稿有什么类似的东西吗?

5 个答案:

答案 0 :(得分:11)

试试ts-morph。只用了大约一个小时,但似乎真的有能力。

import {Project, Scope, SourceFile} from "ts-morph";

const project = new Project();
const sourceFile = project.createSourceFile(`./target/file.ts`);

const classDeclaration = sourceFile.addClass({
    name: 'SomeClass'
});

const constr = classDeclaration.addConstructor({});
const param = constr.addParameter({
  name: 'myProp',
  type: string
});

constr.setBodyText('this.myProp = myProp');

classDeclaration.addProperty({
    name: 'myProp',
    type: 'string',
    initializer: 'hello world!',
    scope: Scope.Public
});
sourceFile.formatText();
console.log(sourceFile.getText());

答案 1 :(得分:5)

  

有什么东西类似于打字稿

尚未,但TypeScript团队正在为插件打开发射器what is that),这将使其成为受支持的方案:https://github.com/Microsoft/TypeScript/issues/5595

答案 2 :(得分:4)

截至2018年10月,您可以为此使用标准TypeScript API

Testing.add_one
print (test)

在这里拍摄: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#user-content-creating-and-printing-a-typescript-ast

答案 3 :(得分:0)

您可以使用此工具使用swagger在Typescript中生成API。 https://github.com/unchase/Unchase.OpenAPI.Connectedservice/

相关: * NSwagStudio

答案 4 :(得分:-1)

当我们需要使用Angular 4和TypeScript添加对使用RESTful API到MEAN堆栈的支持时,我们使用了http://editor.swagger.io并传入了Swagger 2.0 API定义的JSON版本,然后为TypeScript选择了客户端生成器

当然我们作弊了一点,因为我们首先使用SZ Architech(http://www.solution.zone)来创建RESTful API,它使用SwaggerUi来记录生成的API,并允许我们只复制Swagger 2.0定义使用Swagger的代码生成客户端代码。

相关问题