CSharp2TS.Core is a lightweight package that contains the attributes required for the CSharp2TS.CLI dotnet tool to generate TypeScript models and API services.
Add the following attributes to your project to include or exclude items in the CSharp2TS.CLI tool's TypeScript generation.
TSInterface can be added to a class to generate a TypeScript interface.
[TSInterface]
public class TestModel {
...
}TSEnum can be added to enums to generate a TypeScript enum.
[TSEnum]
public enum TestEnum {
...
}TSService can be added to classes which inherit from ControllerBase to generate an api client.
[TSService]
[ApiController]
[Route("api/[controller]")]
public class TestController : ControllerBase {
...
}TSEndpoint can be added to an API endpoint to specify / override the return type.
[HttpGet]
[TSEndpoint(typeof(string))]
public IActionResult Get() {
return Ok("Hello World");
}TSExclude can be added to properties and API endpoints to exclude it from the TypeScript generation.
[TSExclude]
public int ExcludedProperty { get; set; } // Property will not be included in the TypeScript file[HttpGet]
[TSExclude] // Endpoint will not be included in the TypeScript file
public IActionResult Get() {
return Ok("Hello World");
}TSNullable can be added to properties to mark the type as nullable in the TypeScript generation.
[TSNullable]
public string NullableString { get; set; } // Produces nullableString: string | nullTSImport can be added to a controller to include custom TypeScript import statements in the generated service file. This is useful when an endpoint returns a custom type that is not a generated model.
[TSService]
[TSImport("CustomType", "../types/customType")]
[ApiController]
[Route("api/[controller]")]
public class ImportController : ControllerBase {
[HttpGet]
[TSEndpoint("CustomType")]
public IActionResult Get() {
return Ok(...);
}
}This generates the following import in the TypeScript service file:
import CustomType from '../types/customType';Multiple TSImport attributes can be added to a single controller.
TypeName can be passed to TSInterface, TSEnum, or TSService to override the generated TypeScript type name.
[TSInterface("MyCustomName")]
public class TestModel {
...
}Folder can be set on TSInterface, TSEnum, or TSService to place the generated file in a subfolder of the output directory.
[TSInterface(Folder = "subfolder")]
public class TestModel {
...
}IncludeMethods can be passed to TSInterface to include public methods in the generated TypeScript type.
[TSInterface(IncludeMethods = true)]
public class TestModel {
public bool Test() {
return true;
}
...
}GenerateClass can be set on TSInterface to also generate a function that returns a default instance of the interface.
[TSInterface(GenerateClass = true)]
public class TestModel {
...
}TSEndpoint with a string can be used instead of a Type to specify a raw TypeScript return type.
[HttpGet]
[TSEndpoint("CustomType")]
public IActionResult Get() {
return Ok(...);
}