This is a tool that consumes your C# domain models and types and creates TypeScript declaration files from them. There's other tools that does this but what makes this one different is that it internally uses Roslyn (the .NET compiler platform) to parse the source files, which removes the need to create and maintain our own parser.
$ npm install --save csharp-models-to-typescript
- Add a config file to your project that contains for example...
{
"include": [
"./models/**/*.cs",
"./enums/**/*.cs"
],
"exclude": [
"./models/foo/bar.cs"
],
"namespace": "Api",
"output": "./api.d.ts",
"camelCase": false,
"camelCaseEnums": false,
"camelCaseOptions": {
"pascalCase": false,
"preserveConsecutiveUppercase": false,
"locale": "en-US"
},
"numericEnums": false,
"omitFilePathComment": false,
"omitSemicolon": false,
"stringLiteralTypesInsteadOfEnums": false,
"customTypeTranslations": {
"ProductName": "string",
"ProductNumber": "string"
}
}
- Add a npm script to your package.json that references your config file...
"scripts": {
"generate-types": "csharp-models-to-typescript --config=your-config-file.json"
},
- Run the npm script
generate-types
and the output file specified in your config should be created and populated with your models.
MIT © Jonathan Svenheden