Steven Sann
Steven Sann

Reputation: 578

how to use C# enum list in typescript

This is my enum list class in C#

   namespace MyProject.MyName
    {
   public enum MyNameList
     {
    [Description("NameOne")]
    NameOne,

    [Description("NameTwo")]
    NameTwo,

    [Description("NameThree")]
    NameThree
        }
      } 

And this is how I use this enum list inside C# .cshtml razor and javascript

    @using MyProject.MyName
     ....
     ....
     <script>
          ..
          ..
      if (result.data == @((int)MyNameList.NameOne))
          ..
          ..
     </script>  

Now I have to write this (javascript) code in Typescript and how can I use my Enum list MyNameList inside Typescript ??

Upvotes: 3

Views: 4141

Answers (2)

zey
zey

Reputation: 6103

You need to create a new typescript definition file, for example, nameList.d.ts

export enum MyNameList
{
    NameOne,
    NameTwo,
    NameThree
}

Inside Typescript file, import like this

import { MyNameList } from "./nameList";

And now you can use those enum list inside typescript like

MyNameList.NameOne

Upvotes: 2

Manoj Choudhari
Manoj Choudhari

Reputation: 5634

The javascript is executed on the local browser. You would not be able to use Enum directly there.

You can return the Enum value as a part of response and then compare the enum value in JS.

You can add enum in typescript and then use it to compare with the response fields.

enum Direction {
    Up = "UP",
    Down = "DOWN",
    Left = "LEFT",
    Right = "RIGHT",
}

Upvotes: 1

Related Questions