A tool that can generate TypeScript or Flow types from C# classes
See CHANGELOG.
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.
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',
}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.
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.
This option is disabled by default. When enabled, global Nullable<T> is used instead of union null | T
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.
There is ContractGeneratorIgnore attribute that can be applied to properties and makes generator skip current property.