Skip to content

ruo2012/TypeScript.ContractGenerator

 
 

Repository files navigation

TypeScript.ContractGenerator

NuGet Status Build status

A tool that can generate TypeScript or Flow types from C# classes

Release Notes

See CHANGELOG.

How to Use

First, define types that need type generation:

public class FirstType
{
    public string StringProp { get; set; }
    public int IntProp { get; set; }
}

public class SecondType
{
    public string[] StringArray { get; set; }
    public FirstType FirstTypeProp { get; set; }
}

Then generate TypeScript files with:

var generator = new TypeScriptGenerator(TypeScriptGenerationOptions.Default, CustomTypeGenerator.Null, new RootTypesProvider(typeof(SecondType)));
generator.GenerateFiles("./output", JavaScriptTypeChecker.TypeScript);

By default, this will generate file with name .ts with following content:

// tslint:disable
// TypeScriptContractGenerator's generated content

export type SecondType = {
    stringArray?: null | string[];
    firstTypeProp?: null | FirstType;
};
export type FirstType = {
    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.

Generation options

EnumGenerationMode

This options is set to FixedStringsAndDictionary by default.

public enum EnumGenerationMode
{
    FixedStringsAndDictionary = 0,
    TypeScriptEnum = 1,
}

Setting option value equal to FixedStringsAndDictionary produces following output:

export type SomeEnum = 'A' | 'B';
export const SomeEnums = {
    ['A']: ('A') as SomeEnum,
    ['B']: ('B') as SomeEnum,
};

Option value TypeScriptEnum produces following:

export enum SomeEnum {
    A = 'A',
    B = 'B',
}

EnableOptionalProperties

This option is enabled by default. When enabled produces optional properties for members which may contain nulls.

export type SomeType = {
    somePropertyWithNullableValue?: typeDefinition;
    somePropertyWithNonNullableValue: typeDefinition;
};

When disabled, all properties produced as required.

EnableExplicitNullability

This option is enabled by default. When enabled produces nullable types for members which may contain nulls.

export type SomeType = {
    nullablePropertyDefinition: null | string;
    nonNullablePropertyDefinition: string;
};

When disabled produces all types as-is.

UseGlobalNullable

This option is disabled by default. When enabled, global Nullable<T> is used instead of union null | T

NullabilityMode

This option is set to Pessimistic by default. When set to Pessimistic, generates Nullable property for properties that have no nullability attributes. When set to Optimistic, generates not null property for properties that have no nullability attributes.

Attributes

There is ContractGeneratorIgnore attribute that can be applied to properties and makes generator skip current property.

About

A tool that can generate TypeScript or Flow types from C# classes

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 81.8%
  • TypeScript 11.1%
  • JavaScript 6.7%
  • Batchfile 0.4%