TypeSharper 0.1.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package TypeSharper --version 0.1.4
                    
NuGet\Install-Package TypeSharper -Version 0.1.4
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TypeSharper" Version="0.1.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TypeSharper" Version="0.1.4" />
                    
Directory.Packages.props
<PackageReference Include="TypeSharper" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TypeSharper --version 0.1.4
                    
#r "nuget: TypeSharper, 0.1.4"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TypeSharper@0.1.4
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TypeSharper&version=0.1.4
                    
Install as a Cake Addin
#tool nuget:?package=TypeSharper&version=0.1.4
                    
Install as a Cake Tool

TypeSharper

<img src="https://raw.githubusercontent.com/gamli/TypeSharper/master/TypeSharper/TypeSharper/logo.svg" alt="T# Logo" width="200"/>

Overview

Code Generation Mechanism:

TypeSharper employs Roslyn, the .NET Compiler Platform, to facilitate the generation of C# source code. The primary orchestrator of this operation is TypeSharperGenerator, which scans the provided codebase for specific attribute applications that dictate the code generation process. It then delegates the task of generating the code for specific types to individual type generators, such as TaggedUnionGenerator.

These type generators are designed to inherit from TypeGenerator, and they define their unique attributes. These attributes, when applied to partial types (like interfaces or classes), act as markers signaling the TypeSharperGenerator about the types that need code generation.

The generation process doesn't directly interact with the Roslyn API. Instead, it operates on the TsModel API, a simplified Abstract Syntax Tree (AST) that carries semantic information. This makes the code generation process less intricate and more focused on the specific needs of TypeSharper.

Dependency Management:

When a type generator depends on other types, these dependencies are specified as generic parameters of the attribute. This ensures that the TypeSharperGenerator can ascertain the dependencies between types and generate them in the correct order, maintaining the integrity and correctness of the generated code. The TypeDependencyGraph is instrumental in tracking and resolving these dependencies.

Creating a Type Generator

  1. Inherit from TypeGenerator: This is the base class that provides essential functionalities and sets the contract for type generators.
  2. Define a Custom Attribute using AttributeDefinition: Utilize the TypeGenerator.AttributeDefinition() method to define your custom attribute.
  3. Attribute Application: The custom attributes are applied to partial types. The generated code will be another partial of the same type, ensuring the completeness of the type with both manual and generated code.
  4. Specify Dependencies: If your type generator depends on other types, specify them as generic parameters of the attribute. For instance, for a Pick type, the type from which properties are picked is a dependency. This allows the TypeSharperGenerator to determine the order of code generation based on these dependencies.
  5. Leverage TsModel API: The type generator is passed the target partial type, the attribute application, and a TsModel instance. The TsModel instance can be used to look up references of dependent types and add new types. It's important to note that TsModel is immutable, ensuring thread safety and deterministic behavior.
  6. Return the Updated Model: After generating the required code, the type generator returns the updated TsModel. In the end, a diff between the updated and the original model is generated as C# source, which is then integrated into the target project.
  • Generator: Contains the core classes responsible for generating TypeScript-like types from C# code based on specific attributes.
  • Model: Central to TypeSharper, the Model contains the data structures and models used throughout the library. These models represent TypeScript-like constructs and offer methods to generate their C# counterparts.
  • Diagnostics: Provides diagnostic capabilities, allowing for error detection, reporting, and handling. It ensures clear feedback for developers during the source generation process.
  • SemanticExtensions: Contains extension methods that augment Roslyn's semantic models, facilitating the conversion of Roslyn symbols into TypeSharper models.
  • SyntaxExtensions: Offers extension methods that supplement Roslyn's syntax models, aiding in information extraction and type conversion tailored for TypeSharper.

TsList

Similar in spirit to TsDict, TsList wraps .NET's immutable list to make it behave like a value. Its equality members are tailored to compare the actual elements in the list rather than mere references, bringing value semantics to list operations. This ensures a more intuitive and predictable behavior when working with lists, especially in scenarios where content-based comparisons are essential. Though TsList includes a handful of convenience methods, its main distinction is offering value-based behavior to .NET's immutable lists.

TsDict

TsDict serves as a thin wrapper around .NET's immutable dictionary, engineered primarily to make the underlying data structure behave like a value. This means that its equality members are designed to compare the actual contents of the dictionaries rather than just references. By doing so, it introduces value semantics to dictionary operations in C#, enabling developers to reason about dictionaries in terms of their actual contents. While it adds a few convenience methods to ease dictionary operations, the core value of TsDict lies in its provision of true value-based comparisons for immutable dictionaries.

TypeSharperGenerator

The core component of the TypeSharper library. This class leverages the power of Roslyn to generate C# source code, introducing TypeScript-like types and constructs. It acts as a bridge, translating TypeScript idioms into their C# counterparts.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.5 210 9/1/2023
0.1.4 199 9/1/2023
0.1.3 184 8/31/2023
0.1.2 187 8/30/2023
0.1.1 176 8/30/2023
0.1.0 176 8/30/2023