Is there code generation API for TypeScript?

前端 未结 5 2095
不知归路
不知归路 2021-02-12 11:17

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 typescr

相关标签:
5条回答
  • 2021-02-12 11:53

    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

    0 讨论(0)
  • 2021-02-12 11:57

    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

    0 讨论(0)
  • 2021-02-12 12:04

    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());
    
    0 讨论(0)
  • 2021-02-12 12:15

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

    Related: * NSwagStudio

    0 讨论(0)
  • 2021-02-12 12:15

    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.

    0 讨论(0)
提交回复
热议问题