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;
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
add a comment |
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
1
I'm not sure at 100%, but the compiler correctly refuses the cast: what should happen when thea
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
add a comment |
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
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
typescript enums casting
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 thea
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
add a comment |
1
I'm not sure at 100%, but the compiler correctly refuses the cast: what should happen when thea
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
add a comment |
1 Answer
1
active
oldest
votes
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Mar 27 at 10:33
AlbertoFdzMAlbertoFdzM
6468 silver badges21 bronze badges
6468 silver badges21 bronze badges
add a comment |
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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