32

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

2
  • I see you've tagged it as T4 -- T4 works fine with any kind of language since it's basically just plain text. Roslyn is VB.NET and C# only. Commented Apr 4, 2016 at 16:25
  • You can check this codeproject.com/Tips/1166380/… Commented Jan 19, 2017 at 8:53

5 Answers 5

41

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

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

classDeclaration.addProperty({
    name: 'myProp',
    type: 'string',
    initializer: 'hello world!',
    scope: Scope.Public
});
sourceFile.formatText();
console.log(sourceFile.getText());
Sign up to request clarification or add additional context in comments.

3 Comments

After working longer with this library I can confirm its the real deal. Still some bugs, but definitely has a lot of promise. The author has really done a great job
Still in active development and a very nice API. Thanks for the recommendation
As an original asker, I can confirm, that I've ended up using ts-morph. I tried the raw typescript compiler api, but it was too low level and I've ended up writing helper functions that did exacatly what ts-morh does.
27

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

Comments

4

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

Comments

-7

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

Related: * NSwagStudio

Comments

-9

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.