There are many cases when we use code generation. TypeScript-based projects are not exceptions. TypeScript itself has a rich compiler API for these kind of things. The compiler API is not well documented but it’s quite concise and intuitive.

In this article I want to focus on generating type declarations as I find this case very useful when we work with external systems which provide schemas about their types.

To start using the TypeScript compiler API we need a Node.js-based with installed the typescript package. In examples I use TypeScript as the primary language to have a complete IDE experience…

We all know there’s no discriminated union types in C# but if you work with languages like TypeScript you’re already familiar with them. Let model a simple discriminated union using TypeScript:

In C# we have to use a more verbose approach to achieve the same effect due to the nominal type system but after all it’s possible. That’s how the union type above can be implemented using C#:

Let’s try to serialize an instance of this union type using System.Text.Json. …

Max Koverdyaev

Software Developer, Lazy Person

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store