Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there code generation API for TypeScript?

Tags:

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

like image 365
Liero Avatar asked Apr 04 '16 15:04

Liero


People also ask

How is TypeScript code compiled?

The TypeScript compiler compiles these files and outputs the JavaScript with . js extension by keeping the same file name as the individual input file. The TypeScript compiler also preserves the original file path, hence the . js output file will be generated where the input file was in the directory structure.

Does TypeScript generate JavaScript?

TypeScript is a superset of the JavaScript language that has a single open-source compiler and is developed mainly by a single vendor: Microsoft. The goal of TypeScript is to help catch mistakes early through a type system and to make JavaScript development more efficient.

Does TypeScript have a compiler?

Compiler. The TypeScript compiler, named tsc , is written in TypeScript. As a result, it can be compiled into regular JavaScript and can then be executed in any JavaScript engine (e.g. a browser).

What Is A TypeScript client?

The Typescript client library allows you to integrate FusionAuth with your JavaScript application. Regardless of the fact that this is written in TypeScript, this client supports both NodeJS and Browser environments without requiring that your application is also written in typescript.


1 Answers

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()); 
like image 200
NSjonas Avatar answered Nov 08 '22 13:11

NSjonas