How to map an enum to another enum in typescript?What does the [Flags] Enum Attribute mean in C#?Cast int to enum in C#How can I represent an 'Enum' in Python?How do I enumerate an enum in C#?What is the preferred syntax for defining enums in JavaScript?How to get an enum value from a string value in Java?Get int value from enum in C#How to loop through all enum values in C#?Comparing Java enum members: == or equals()?What is TypeScript and why would I use it in place of JavaScript?

Combining 3D graphics with different lighting conditions

If I animate and control a zombie, does it benefit from Undead Fortitude when it's reduced to 0 HP?

If you know the location of an invisible creature, can you attack it?

Tempoverlustspiel

Why did Saruman lie?

Should I email my professor about a recommendation letter if he has offered me a job?

How can I find an old paper when the usual methods fail?

What is the status of this patent?

How to "add" units to results of pgfmathsetmacro?

The cat ate your input again!

Will using a resistor in series with a LED to control its voltage increase the total energy expenditure?

How can I communicate my issues with a potential date's pushy behavior?

Does fossil fuels use since 1990 account for half of all the fossil fuels used in history?

Why are Tucker and Malcolm not dead?

(A room / an office) where an artist works

How to remove ambiguity: "... lives in the city of H, the capital of the province of NS, WHERE the unemployment rate is ..."?

What would it take to get a message to another star?

What is a "soap"?

Modeling the uncertainty of the input parameters

In which case does the Security misconfiguration vulnerability apply to?

How do you deal with the emotions of not being the one to find the cause of a bug?

What kind of liquid can be seen 'leaking' from the upper surface of the wing of a Boeing 737-800?

Do I have to cite common CS algorithms?

Why is the Lucas test not recommended to differentiate higher alcohols?



How to map an enum to another enum in typescript?


What does the [Flags] Enum Attribute mean in C#?Cast int to enum in C#How can I represent an 'Enum' in Python?How do I enumerate an enum in C#?What is the preferred syntax for defining enums in JavaScript?How to get an enum value from a string value in Java?Get int value from enum in C#How to loop through all enum values in C#?Comparing Java enum members: == or equals()?What is TypeScript and why would I use it in place of JavaScript?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I want to map one object's property that has an enum type to another object's property that is other enum type.



I've tried with type1.a as Enum2 or Enum2[type1.a] with no success.



Here is my simplified code problem:



enum Enum1 
N = 0,
A = 1,
B = 2


enum Enum2
A = 1,
B = 2


interface Type1
a: Enum1;


interface Type2
a: Enum2;


const type1: Type1 =
a: Enum1.A
;

const type2: Type2 =
a: type1.a
;


try it



Throws the error:



Type 'Enum1' is not assignable to type 'Enum2'.
(property) Type2.a: Enum2









share|improve this question





















  • 1





    I'm not sure at 100%, but the compiler correctly refuses the cast: what should happen when the a field becomes "N"? the Enum2 does not allow "N" as possible value. However, if you really want to cast two incompatible types, you may cast one to "any" first, then follow a second cast to Enum2...at your own risk!

    – Mario Vernari
    Mar 27 at 10:16

















1















I want to map one object's property that has an enum type to another object's property that is other enum type.



I've tried with type1.a as Enum2 or Enum2[type1.a] with no success.



Here is my simplified code problem:



enum Enum1 
N = 0,
A = 1,
B = 2


enum Enum2
A = 1,
B = 2


interface Type1
a: Enum1;


interface Type2
a: Enum2;


const type1: Type1 =
a: Enum1.A
;

const type2: Type2 =
a: type1.a
;


try it



Throws the error:



Type 'Enum1' is not assignable to type 'Enum2'.
(property) Type2.a: Enum2









share|improve this question





















  • 1





    I'm not sure at 100%, but the compiler correctly refuses the cast: what should happen when the a field becomes "N"? the Enum2 does not allow "N" as possible value. However, if you really want to cast two incompatible types, you may cast one to "any" first, then follow a second cast to Enum2...at your own risk!

    – Mario Vernari
    Mar 27 at 10:16













1












1








1


1






I want to map one object's property that has an enum type to another object's property that is other enum type.



I've tried with type1.a as Enum2 or Enum2[type1.a] with no success.



Here is my simplified code problem:



enum Enum1 
N = 0,
A = 1,
B = 2


enum Enum2
A = 1,
B = 2


interface Type1
a: Enum1;


interface Type2
a: Enum2;


const type1: Type1 =
a: Enum1.A
;

const type2: Type2 =
a: type1.a
;


try it



Throws the error:



Type 'Enum1' is not assignable to type 'Enum2'.
(property) Type2.a: Enum2









share|improve this question
















I want to map one object's property that has an enum type to another object's property that is other enum type.



I've tried with type1.a as Enum2 or Enum2[type1.a] with no success.



Here is my simplified code problem:



enum Enum1 
N = 0,
A = 1,
B = 2


enum Enum2
A = 1,
B = 2


interface Type1
a: Enum1;


interface Type2
a: Enum2;


const type1: Type1 =
a: Enum1.A
;

const type2: Type2 =
a: type1.a
;


try it



Throws the error:



Type 'Enum1' is not assignable to type 'Enum2'.
(property) Type2.a: Enum2






typescript enums casting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 2 at 8:44









AlbertoFdzM

6468 silver badges21 bronze badges




6468 silver badges21 bronze badges










asked Mar 27 at 10:07









Alex WeheAlex Wehe

185 bronze badges




185 bronze badges










  • 1





    I'm not sure at 100%, but the compiler correctly refuses the cast: what should happen when the a field becomes "N"? the Enum2 does not allow "N" as possible value. However, if you really want to cast two incompatible types, you may cast one to "any" first, then follow a second cast to Enum2...at your own risk!

    – Mario Vernari
    Mar 27 at 10:16












  • 1





    I'm not sure at 100%, but the compiler correctly refuses the cast: what should happen when the a field becomes "N"? the Enum2 does not allow "N" as possible value. However, if you really want to cast two incompatible types, you may cast one to "any" first, then follow a second cast to Enum2...at your own risk!

    – Mario Vernari
    Mar 27 at 10:16







1




1





I'm not sure at 100%, but the compiler correctly refuses the cast: what should happen when the a field becomes "N"? the Enum2 does not allow "N" as possible value. However, if you really want to cast two incompatible types, you may cast one to "any" first, then follow a second cast to Enum2...at your own risk!

– Mario Vernari
Mar 27 at 10:16





I'm not sure at 100%, but the compiler correctly refuses the cast: what should happen when the a field becomes "N"? the Enum2 does not allow "N" as possible value. However, if you really want to cast two incompatible types, you may cast one to "any" first, then follow a second cast to Enum2...at your own risk!

– Mario Vernari
Mar 27 at 10:16












1 Answer
1






active

oldest

votes


















0














You need to cast the type1.a prop to Enum2. To do this you need to use a "Type Guard".



Here is an example with your code:



enum Enum1 
N = 0,
A = 1,
B = 2


enum Enum2
A = 1,
B = 2


interface Type1
a: Enum1;


interface Type2
a: Enum2;


const type1: Type1 =
a: Enum1.N
;

function isEnum2(value: any): value is Enum2
let isEnum2 = false;

for (let key in Enum2)
if (Enum2[key] === value)
isEnum2 = true;



return isEnum2;


if (isEnum2(type1.a))
const type2: Type2 =
a: type1.a
;



try it



When the isEnum2 function is used in the if block the prop type1.a becomes of type Enum2 inside that if block only.



Note: you could replace the content in the isEnum2 using for..in loop if you are using ES2017 or higher using Object.values:



return Object.values(Enum2).indexOf(value) > -1;


try it (Doesn't work in www.typescriptlang.org)



More info about Type Guards






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55374588%2fhow-to-map-an-enum-to-another-enum-in-typescript%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You need to cast the type1.a prop to Enum2. To do this you need to use a "Type Guard".



    Here is an example with your code:



    enum Enum1 
    N = 0,
    A = 1,
    B = 2


    enum Enum2
    A = 1,
    B = 2


    interface Type1
    a: Enum1;


    interface Type2
    a: Enum2;


    const type1: Type1 =
    a: Enum1.N
    ;

    function isEnum2(value: any): value is Enum2
    let isEnum2 = false;

    for (let key in Enum2)
    if (Enum2[key] === value)
    isEnum2 = true;



    return isEnum2;


    if (isEnum2(type1.a))
    const type2: Type2 =
    a: type1.a
    ;



    try it



    When the isEnum2 function is used in the if block the prop type1.a becomes of type Enum2 inside that if block only.



    Note: you could replace the content in the isEnum2 using for..in loop if you are using ES2017 or higher using Object.values:



    return Object.values(Enum2).indexOf(value) > -1;


    try it (Doesn't work in www.typescriptlang.org)



    More info about Type Guards






    share|improve this answer





























      0














      You need to cast the type1.a prop to Enum2. To do this you need to use a "Type Guard".



      Here is an example with your code:



      enum Enum1 
      N = 0,
      A = 1,
      B = 2


      enum Enum2
      A = 1,
      B = 2


      interface Type1
      a: Enum1;


      interface Type2
      a: Enum2;


      const type1: Type1 =
      a: Enum1.N
      ;

      function isEnum2(value: any): value is Enum2
      let isEnum2 = false;

      for (let key in Enum2)
      if (Enum2[key] === value)
      isEnum2 = true;



      return isEnum2;


      if (isEnum2(type1.a))
      const type2: Type2 =
      a: type1.a
      ;



      try it



      When the isEnum2 function is used in the if block the prop type1.a becomes of type Enum2 inside that if block only.



      Note: you could replace the content in the isEnum2 using for..in loop if you are using ES2017 or higher using Object.values:



      return Object.values(Enum2).indexOf(value) > -1;


      try it (Doesn't work in www.typescriptlang.org)



      More info about Type Guards






      share|improve this answer



























        0












        0








        0







        You need to cast the type1.a prop to Enum2. To do this you need to use a "Type Guard".



        Here is an example with your code:



        enum Enum1 
        N = 0,
        A = 1,
        B = 2


        enum Enum2
        A = 1,
        B = 2


        interface Type1
        a: Enum1;


        interface Type2
        a: Enum2;


        const type1: Type1 =
        a: Enum1.N
        ;

        function isEnum2(value: any): value is Enum2
        let isEnum2 = false;

        for (let key in Enum2)
        if (Enum2[key] === value)
        isEnum2 = true;



        return isEnum2;


        if (isEnum2(type1.a))
        const type2: Type2 =
        a: type1.a
        ;



        try it



        When the isEnum2 function is used in the if block the prop type1.a becomes of type Enum2 inside that if block only.



        Note: you could replace the content in the isEnum2 using for..in loop if you are using ES2017 or higher using Object.values:



        return Object.values(Enum2).indexOf(value) > -1;


        try it (Doesn't work in www.typescriptlang.org)



        More info about Type Guards






        share|improve this answer













        You need to cast the type1.a prop to Enum2. To do this you need to use a "Type Guard".



        Here is an example with your code:



        enum Enum1 
        N = 0,
        A = 1,
        B = 2


        enum Enum2
        A = 1,
        B = 2


        interface Type1
        a: Enum1;


        interface Type2
        a: Enum2;


        const type1: Type1 =
        a: Enum1.N
        ;

        function isEnum2(value: any): value is Enum2
        let isEnum2 = false;

        for (let key in Enum2)
        if (Enum2[key] === value)
        isEnum2 = true;



        return isEnum2;


        if (isEnum2(type1.a))
        const type2: Type2 =
        a: type1.a
        ;



        try it



        When the isEnum2 function is used in the if block the prop type1.a becomes of type Enum2 inside that if block only.



        Note: you could replace the content in the isEnum2 using for..in loop if you are using ES2017 or higher using Object.values:



        return Object.values(Enum2).indexOf(value) > -1;


        try it (Doesn't work in www.typescriptlang.org)



        More info about Type Guards







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 27 at 10:33









        AlbertoFdzMAlbertoFdzM

        6468 silver badges21 bronze badges




        6468 silver badges21 bronze badges





















            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55374588%2fhow-to-map-an-enum-to-another-enum-in-typescript%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현