A tool that can generate TypeScript types from C# classes
| Build Status | |
|---|---|
| TypeScript.ContractGenerator | |
| TypeScript.ContractGenerator.Roslyn | |
| TypeScript.ContractGenerator.Cli | |
| Build |
See CHANGELOG.
First, define types that need type generation:
publicclassFirstType{publicstringStringProp{get;set;}publicintIntProp{get;set;}}publicclassSecondType{publicstring[]StringArray{get;set;}publicFirstTypeFirstTypeProp{get;set;}}Then generate TypeScript files with:
vargenerator=newTypeScriptGenerator(TypeScriptGenerationOptions.Default,CustomTypeGenerator.Null,newRootTypesProvider(typeof(SecondType)));generator.GenerateFiles("./output");By default, this will generate file with name .ts with following content:
// tslint:disable// TypeScriptContractGenerator's generated contentexporttypeSecondType={stringArray?: null|string[];firstTypeProp?: null|FirstType;};exporttypeFirstType={stringProp?: null|string;intProp: number;};If you want generated files to have different name or to generate some typings differently, you should pass your own implementation of ICustomTypeGenerator to TypeScriptGenerator.
Use /* eslint-disable */ or // tslint:disable comment in generated files. EsLint is default option.
This option is enabled by default. When enabled produces optional properties for members which may contain nulls.
exporttypeSomeType={somePropertyWithNullableValue?: typeDefinition;somePropertyWithNonNullableValue: typeDefinition;};When disabled, all properties produced as required.
This option is disabled by default. When enabled, global Nullable<T> is used instead of union null | T
NullabilityMode has 4 options:
- None - all generated properties are not null
- Pessimistic (default) - generates
Nullableproperty for properties that have no JetBrains nullability attributes - Optimistic - generates not null property for properties that have no JetBrains nullability attributes
- NullableReference - generates not null properties based on C# 8 Nullable Reference Type attributes
Options Pessimistic or Optimistic can be combined with NullableReference option, this way generator first looks for C# 8 Nullable Reference Type attributes, then JetBrains nullability attributes
There is ContractGeneratorIgnore attribute that can be applied to properties and makes generator skip current property.
To use Roslyn you should get a Compilation object of your project. It can be done with helper method AdhocProject.GetCompilation(string[] directories, string[] assemblies).
You can then get customization info from this compilation by calling extension method compilation.GetCustomization() and call TypeScriptGenerator with this customization:
var(customTypeGenerator,typesProvider)=AdhocProject.GetCompilation(directories,assemblies).GetCustomization();vartypeGenerator=newTypeScriptGenerator(options,customTypeGenerator,typesProvider);typeGenerator.GenerateFiles(outputDirectory);Install tool with command:
dotnet tool install -g SkbKontur.TypeScript.ContractGenerator.Cli
Use tool with command:
dotnet ts-gen --assembly ./Api/bin/Api.dll --outputDir ./src/Api
dotnet tool also supports Roslyn:
dotnet ts-gen --directory ./Api;./Core --assembly ./External/Dependency.dll --outputDir ./src/Api