Skip to content

IvanJosipovic/BlazorInteropGenerator

Repository files navigation

BlazorInteropGenerator

Nuget Nuget) codecov

What is this?

This project contains code and a Source Generator which can convert a TypeScript Definition to C# Interfaces.

How to use the Source Generator

Create a C# Class Library Project. Add the *.d.ts files that you want converted, also download all the dependencies. File names should be the {PackageName}.d.ts without the Organization/. Update the .csproj with the following settings.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <LangVersion>latest</LangVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="BlazorInteropGenerator.SourceGenerator" Version="1.0.0-*" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
    <AdditionalFiles Include="*.d.ts" />
  </ItemGroup>

</Project>

Create a .cs file with this format, in this case, the TypeScript Definition must contain an interface called "InterfaceName" The Source Generator will generate the interface and all its dependencies.

using BlazorInteropGenerator;

namespace BlazorInteropGenerator.Sample;

[BlazorInteropGenerator("TSD1.d.ts")]
public partial interface InterfaceName
{

}