3

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 ??

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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",
}

2 Comments

do I have to create new enum list for Typescript ?? can I map to C# enum list ??
@StevenSann You cannot use C# code in typescript. This is becuase your server side code is never sent in the response to browser. You will have to create new Enum. If your original enum changes then typescript enum should also be changed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.