A flexible CSharp to TypeScript generator that is Gulp, Webpack and Grunt friendly, written in TypeScript.
npm install @fluffy-spoon/csharp-to-typescript-generatorA flexible CSharp to TypeScript generator that is Gulp, Webpack and Grunt friendly, written in TypeScript.
Uses the following library for parsing C# code from TypeScript: https://github.com/ffMathy/FluffySpoon.JavaScript.CSharpParser
The following code shows general usage. The examples below only differ in the EmitOptions provided.
``typescript
import { Emitter } from '@fluffy-spoon/csharp-to-typescript-generator';
var csharpCode = "insert the CSharp model code here - you could also read it from a file.";
var emitter = new Emitter(csharpCode);
var options =
defaults:
file:
};
var typescriptCode = emitter.emitFile(options);
`
- To see the definitions of each C# type such as CSharpType, look here: https://github.com/ffMathy/FluffySpoon.JavaScript.CSharpParser/blob/master/dist/src/Models.tsFileEmitOptions
- To see the definitions of each option type such as , look here: https://github.com/ffMathy/FluffySpoon.JavaScript.CSharpToTypeScriptGenerator/blob/master/dist/src/Index.d.ts
typescript
var typescriptCode = emitter.emit();
`Given the following CSharp model code:
`csharp
namespace MyNamespace {
public class MyClass {
public bool myField;
public int MyProperty { get; set; }
public string MyOtherProperty { get; set; }
public double? MyNullableProperty { get; set; }
public class MySubclass {
public List MyListProperty { get; set; }
public MyGenericType MyGenericProperty { get; set; }
public Task MyFunction(string input1, int input2) {
//some code
}
}
}
}
`The following TypeScript code would be generated:
`typescript
declare namespace MyNamespace {
interface MyClass {
myField: boolean;
myProperty: number;
myOtherProperty: string;
myNullableProperty?: number;
}
namespace MyClass {
interface MySubclass {
myListProperty: string[];
myGenericProperty: MyGenericType;
MyFunction(input1: string, input2: number): Promise;
}
}
}
``But this framework is flexible! Look at the recipes to get inspiration, or the other configurations available.