Is there code generation API for TypeScript?

旧城冷巷雨未停 提交于 2020-01-01 02:07:26

问题


When I needed to generate some C# code, for example DTO classes from xsd schema, or an excel table, I've used some roslyn API's.

Is there something simmilar for typescript?


回答1:


as of Oct-2018 You could use standard TypeScript API for that

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);

Taken here: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#user-content-creating-and-printing-a-typescript-ast




回答2:


Try ts-morph. Only been working with it for about an hour but it seems really capable.

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());



回答3:


Is there something simmilar for typescript

Not yet, but the TypeScript team is opening up the emitter (what is that) for plugins that would make this a supported scenario : https://github.com/Microsoft/TypeScript/issues/5595




回答4:


You can use this tool to generate API in Typescript using swagger. https://github.com/unchase/Unchase.OpenAPI.Connectedservice/

Related: * NSwagStudio




回答5:


When we needed to add support for consuming our RESTful APIs to a MEAN stack using Angular 4 and TypeScript, we used http://editor.swagger.io and passed in the JSON version of a Swagger 2.0 API definition, then selected client generator for TypeScript.

Of course we cheated a little, in that we used SZ Architech (http://www.solution.zone) to create the RESTful APIs in the first place, which uses SwaggerUi to document the generated APIs, and allows us to just copy the Swagger 2.0 definition to use Swagger's code generation for the client code.



来源:https://stackoverflow.com/questions/36407154/is-there-code-generation-api-for-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!