【问题标题】:Is there code generation API for TypeScript?TypeScript 是否有代码生成 API?
【发布时间】:2016-07-24 06:14:53
【问题描述】:

当我需要生成一些 C# 代码时,例如来自 xsd 架构或 excel 表的 DTO 类,我使用了一些 roslyn API。

打字稿有类似的东西吗?

[编辑]:我最终使用了ts-morph

【问题讨论】:

  • 我看到您已将其标记为 T4 —— T4 适用于任何类型的语言,因为它基本上只是纯文本。 Roslyn 仅支持 VB.NET 和 C#。

标签: typescript code-generation t4 roslyn


【解决方案1】:

试试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({});

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

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

【讨论】:

  • 在使用这个库更长时间后,我可以确认它是真正的交易。仍然有一些错误,但肯定有很多希望。作者真的做得很好
  • 仍在积极开发中,并且是一个非常好的 API。谢谢推荐
  • 作为原始提问者,我可以确认,我最终使用了 ts-morph。我尝试了原始的 typescript 编译器 api,但它的级别太低了,我最终编写了完全符合 ts-morh 的辅助函数。
【解决方案2】:

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

import ts = require("typescript");

function makeFactorialFunction() {
  const functionName = ts.createIdentifier("factorial");
  const paramName = ts.createIdentifier("n");
  const parameter = ts.createParameter(
    /*decorators*/ undefined,
    /*modifiers*/ undefined,
    /*dotDotDotToken*/ undefined,
    paramName
  );

  const condition = ts.createBinary(
    paramName,
    ts.SyntaxKind.LessThanEqualsToken,
    ts.createLiteral(1)
  );

  const ifBody = ts.createBlock(
    [ts.createReturn(ts.createLiteral(1))],
    /*multiline*/ true
  );
  const decrementedArg = ts.createBinary(
    paramName,
    ts.SyntaxKind.MinusToken,
    ts.createLiteral(1)
  );
  const recurse = ts.createBinary(
    paramName,
    ts.SyntaxKind.AsteriskToken,
    ts.createCall(functionName, /*typeArgs*/ undefined, [decrementedArg])
  );
  const statements = [ts.createIf(condition, ifBody), ts.createReturn(recurse)];

  return ts.createFunctionDeclaration(
    /*decorators*/ undefined,
    /*modifiers*/ [ts.createToken(ts.SyntaxKind.ExportKeyword)],
    /*asteriskToken*/ undefined,
    functionName,
    /*typeParameters*/ undefined,
    [parameter],
    /*returnType*/ ts.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
    ts.createBlock(statements, /*multiline*/ true)
  );
}

const resultFile = ts.createSourceFile(
  "someFileName.ts",
  "",
  ts.ScriptTarget.Latest,
  /*setParentNodes*/ false,
  ts.ScriptKind.TS
);
const printer = ts.createPrinter({
  newLine: ts.NewLineKind.LineFeed
});
const result = printer.printNode(
  ts.EmitHint.Unspecified,
  makeFactorialFunction(),
  resultFile
);

console.log(result);

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

【讨论】:

    【解决方案3】:

    打字稿有什么相似之处

    目前还没有,但 TypeScript 团队正在为插件开放 emitter (what is that),这将使其成为受支持的场景:https://github.com/Microsoft/TypeScript/issues/5595

    【讨论】:

      【解决方案4】:

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

      相关: * NSwagStudio

      【讨论】:

        【解决方案5】:

        当我们需要使用 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 的代码生成。

        【讨论】:

          猜你喜欢
          • 2020-02-10
          • 2018-06-30
          • 2011-04-14
          • 1970-01-01
          • 2012-05-02
          • 2020-12-03
          • 2010-09-18
          • 2012-09-22
          相关资源
          最近更新 更多