TypescriptEnumGenerator 0.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package TypescriptEnumGenerator --version 0.2.0
                    
NuGet\Install-Package TypescriptEnumGenerator -Version 0.2.0
                    
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="TypescriptEnumGenerator" Version="0.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TypescriptEnumGenerator" Version="0.2.0" />
                    
Directory.Packages.props
<PackageReference Include="TypescriptEnumGenerator" />
                    
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 TypescriptEnumGenerator --version 0.2.0
                    
#r "nuget: TypescriptEnumGenerator, 0.2.0"
                    
#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 TypescriptEnumGenerator@0.2.0
                    
#: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=TypescriptEnumGenerator&version=0.2.0
                    
Install as a Cake Addin
#tool nuget:?package=TypescriptEnumGenerator&version=0.2.0
                    
Install as a Cake Tool

Typescript Enum Generator

TypescriptEnumGenerator is a package that utilizes the dotnet ISourceGenerator to provide enum types and helper methods for enums. It helps to reduce the overhead of modifying enums throughout your front and back end by generating Typescript types and classes. This assists in maintaining uniformity between C# and TypeScript.

Features

  • Helps maintain uniformity between C# and TypeScript enum definitions.
  • Provides a clean mechanism for utilizing enums for front end dropdowns, while providing compatibility for most component libraries.
  • Integrates into the [Display] attribute and respects usage of [Display(Name="")

Install

dotnet add package TypescriptEnumGenerator --version 0.2.0
Install-Package TypescriptEnumGenerator -Version 0.2.0

Example

Given the following input code

[TypescriptEnum] //Decorate your enum with this little badger 
public enum TestEnum
{
    Active,
    Closed,
    [Display(Name = "Something else")]
    SomethingElse = 13,
    [Display(Name = "Hello it's magic")]
    HelloItsMagic
}

And the system will generate the following typescript file

import DropDownOption from './DropDownOption'
export enum TestEnum {
    Active = 0,
    Closed = 1,
    SomethingElse = 13,
    HelloItsMagic = 14,
}

export function getTestEnumFromInt(value: number): TestEnum {
    switch (value) {

        case 0: return TestEnum.Active;
        case 1: return TestEnum.Closed;
        case 13: return TestEnum.SomethingElse;
        case 14: return TestEnum.HelloItsMagic;

        default: throw new Error(`Unknown TestEnum value: ${value}`);
    }
}


export function getTestEnumFromString(value: string): TestEnum {
    switch (value.toLowerCase()) {

        case "active": return TestEnum.Active;
        case "closed": return TestEnum.Closed;
        case "somethingelse": return TestEnum.SomethingElse;
        case "something else": return TestEnum.SomethingElse;
        case "helloitsmagic": return TestEnum.HelloItsMagic;
        case "hello it's magic": return TestEnum.HelloItsMagic;

        default: throw new Error(`Unknown TestEnum value: ${value}`);
    }
}

export function getTestEnumDropDownOptions(): DropDownOption[] {
    const dropDownOptions = new Array<DropDownOption>();
    dropDownOptions.push(new DropDownOption(0, "Active"));
    dropDownOptions.push(new DropDownOption(1, "Closed"));
    dropDownOptions.push(new DropDownOption(13, "Something else"));
    dropDownOptions.push(new DropDownOption(14, "Hello it's magic"));
    return dropDownOptions;
}

The generator will also generate a single DropDownOption.ts file that is utilized for the GetEnumDropDownOptions method, this is designed to be used with Select and DropDown components provided by most component libraries and the vanilla HTML specification.

export default class DropdownOption {
    id: number;
    label: string;

    constructor(id: number, label: string) {
        this.id = id;
        this.label = label;
    }
}

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 netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  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.

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.2.1 182 6/9/2024
0.2.0 163 6/8/2024
0.1.0 160 6/7/2024