Search code examples
typescriptcode-generationt4roslyn

Is there code generation API for TypeScript?


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?

[EDIT]: I've end up using ts-morph


Solution

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

    import ts = require("typescript");
    
    function makeFactorialFunction() {
      const functionName = ts.factory.createIdentifier("factorial");
      const paramName = ts.factory.createIdentifier("n");
      const parameter = ts.factory.createParameterDeclaration(
        /*decorators*/ undefined,
        /*modifiers*/ undefined,
        /*dotDotDotToken*/ undefined,
        paramName
      );
    
      const condition = ts.factory.createBinaryExpression(
        paramName,
        ts.SyntaxKind.LessThanEqualsToken,
        ts.factory.createNumericLiteral(1)
      );
    
      const ifBody = ts.factory.createBlock(
        [ts.factory.createReturnStatement(ts.factory.createNumericLiteral(1))],
        /*multiline*/ true
      );
      const decrementedArg = ts.factory.createBinaryExpression(
        paramName,
        ts.SyntaxKind.MinusToken,
        ts.factory.createNumericLiteral(1)
      );
      const recurse = ts.factory.createBinaryExpression(
        paramName,
        ts.SyntaxKind.AsteriskToken,
        ts.factory.createCallExpression(functionName, /*typeArgs*/ undefined, [decrementedArg])
      );
      const statements = [ts.factory.createIfStatement(condition, ifBody), ts.factory.createReturnStatement(recurse)];
    
      return ts.factory.createFunctionDeclaration(
        /*decorators*/ undefined,
        /*modifiers*/ [ts.factory.createToken(ts.SyntaxKind.ExportKeyword)],
        /*asteriskToken*/ undefined,
        functionName,
        /*typeParameters*/ undefined,
        [parameter],
        /*returnType*/ ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
        ts.factory.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