Convert string to Typescript enum in Angular 6 template statement












1















I have a simple Angular 6 app with an Angular Material select component which I'd like to two-way bind to a enum variable. To do this I'll need to convert between string value which is used to identify the options and enum value which is the actual Typescript type. To convert string to enum I have always done this (assuming Value1 is a member of MyEnum):



let s: string = "Value1";
let myEnumVariable: MyEnum = MyEnum[s];


but this doesn't seem to work in Angular template statements, e.g. for the following:



app.component.html:



<mat-form-field>
<mat-select placeholder="Select a value" [value]="MyEnum[myVariable]" (valueChange)="myVariable = MyEnum[$event]">
<mat-option value="Value1">Value 1</mat-option>
<mat-option value="Value2">Value 2</mat-option>
</mat-select>
</mat-form-field>


app.component.ts:



import { Component } from '@angular/core';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app';
public myVariable: MyEnum = MyEnum.Value1;
public MyEnum = MyEnum;
}

enum MyEnum {
Value1,
Value2
}


Everything is fine when developing with Angular development server. But once I run ng build --prod, this error appears:




ERROR in srcappapp.component.html(2,73): : Type 'string' is not
assignable to type 'MyEnum'.




Why would this appear in building but not development? Any idea how to fix this, without using an explicit method call in the template statement?



Thanks a lot!



P.S I'm using Typescript 2.7.2










share|improve this question

























  • (change)="myVariable = $any(MyEnum[$event]);"

    – yurzui
    Nov 15 '18 at 5:05











  • @yurzui This fixes it! But Angular in VS code gives a static checking error - [Angular] Unknown method '$any'. Any way to remove that? And if you wish you may post this as an answer and I can mark it as accepted :)

    – scharnyw
    Nov 15 '18 at 5:14











  • It looks like Angular language service doesn't support $any yet but AOT does angular.io/guide/aot-compiler#disabling-type-checking-using-any

    – yurzui
    Nov 15 '18 at 5:17











  • This is why it doesn't work in aot. stackblitz.com/edit/angular-bpgmal?file=src/app/… AOT handles $event as any.

    – yurzui
    Nov 15 '18 at 5:20











  • Another option you can try is (change)="myVariable = MyEnum['' + $event];"

    – yurzui
    Nov 15 '18 at 5:22
















1















I have a simple Angular 6 app with an Angular Material select component which I'd like to two-way bind to a enum variable. To do this I'll need to convert between string value which is used to identify the options and enum value which is the actual Typescript type. To convert string to enum I have always done this (assuming Value1 is a member of MyEnum):



let s: string = "Value1";
let myEnumVariable: MyEnum = MyEnum[s];


but this doesn't seem to work in Angular template statements, e.g. for the following:



app.component.html:



<mat-form-field>
<mat-select placeholder="Select a value" [value]="MyEnum[myVariable]" (valueChange)="myVariable = MyEnum[$event]">
<mat-option value="Value1">Value 1</mat-option>
<mat-option value="Value2">Value 2</mat-option>
</mat-select>
</mat-form-field>


app.component.ts:



import { Component } from '@angular/core';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app';
public myVariable: MyEnum = MyEnum.Value1;
public MyEnum = MyEnum;
}

enum MyEnum {
Value1,
Value2
}


Everything is fine when developing with Angular development server. But once I run ng build --prod, this error appears:




ERROR in srcappapp.component.html(2,73): : Type 'string' is not
assignable to type 'MyEnum'.




Why would this appear in building but not development? Any idea how to fix this, without using an explicit method call in the template statement?



Thanks a lot!



P.S I'm using Typescript 2.7.2










share|improve this question

























  • (change)="myVariable = $any(MyEnum[$event]);"

    – yurzui
    Nov 15 '18 at 5:05











  • @yurzui This fixes it! But Angular in VS code gives a static checking error - [Angular] Unknown method '$any'. Any way to remove that? And if you wish you may post this as an answer and I can mark it as accepted :)

    – scharnyw
    Nov 15 '18 at 5:14











  • It looks like Angular language service doesn't support $any yet but AOT does angular.io/guide/aot-compiler#disabling-type-checking-using-any

    – yurzui
    Nov 15 '18 at 5:17











  • This is why it doesn't work in aot. stackblitz.com/edit/angular-bpgmal?file=src/app/… AOT handles $event as any.

    – yurzui
    Nov 15 '18 at 5:20











  • Another option you can try is (change)="myVariable = MyEnum['' + $event];"

    – yurzui
    Nov 15 '18 at 5:22














1












1








1








I have a simple Angular 6 app with an Angular Material select component which I'd like to two-way bind to a enum variable. To do this I'll need to convert between string value which is used to identify the options and enum value which is the actual Typescript type. To convert string to enum I have always done this (assuming Value1 is a member of MyEnum):



let s: string = "Value1";
let myEnumVariable: MyEnum = MyEnum[s];


but this doesn't seem to work in Angular template statements, e.g. for the following:



app.component.html:



<mat-form-field>
<mat-select placeholder="Select a value" [value]="MyEnum[myVariable]" (valueChange)="myVariable = MyEnum[$event]">
<mat-option value="Value1">Value 1</mat-option>
<mat-option value="Value2">Value 2</mat-option>
</mat-select>
</mat-form-field>


app.component.ts:



import { Component } from '@angular/core';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app';
public myVariable: MyEnum = MyEnum.Value1;
public MyEnum = MyEnum;
}

enum MyEnum {
Value1,
Value2
}


Everything is fine when developing with Angular development server. But once I run ng build --prod, this error appears:




ERROR in srcappapp.component.html(2,73): : Type 'string' is not
assignable to type 'MyEnum'.




Why would this appear in building but not development? Any idea how to fix this, without using an explicit method call in the template statement?



Thanks a lot!



P.S I'm using Typescript 2.7.2










share|improve this question
















I have a simple Angular 6 app with an Angular Material select component which I'd like to two-way bind to a enum variable. To do this I'll need to convert between string value which is used to identify the options and enum value which is the actual Typescript type. To convert string to enum I have always done this (assuming Value1 is a member of MyEnum):



let s: string = "Value1";
let myEnumVariable: MyEnum = MyEnum[s];


but this doesn't seem to work in Angular template statements, e.g. for the following:



app.component.html:



<mat-form-field>
<mat-select placeholder="Select a value" [value]="MyEnum[myVariable]" (valueChange)="myVariable = MyEnum[$event]">
<mat-option value="Value1">Value 1</mat-option>
<mat-option value="Value2">Value 2</mat-option>
</mat-select>
</mat-form-field>


app.component.ts:



import { Component } from '@angular/core';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app';
public myVariable: MyEnum = MyEnum.Value1;
public MyEnum = MyEnum;
}

enum MyEnum {
Value1,
Value2
}


Everything is fine when developing with Angular development server. But once I run ng build --prod, this error appears:




ERROR in srcappapp.component.html(2,73): : Type 'string' is not
assignable to type 'MyEnum'.




Why would this appear in building but not development? Any idea how to fix this, without using an explicit method call in the template statement?



Thanks a lot!



P.S I'm using Typescript 2.7.2







angular typescript angular-material angular-template






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 4:22







scharnyw

















asked Nov 15 '18 at 3:08









scharnywscharnyw

3017




3017













  • (change)="myVariable = $any(MyEnum[$event]);"

    – yurzui
    Nov 15 '18 at 5:05











  • @yurzui This fixes it! But Angular in VS code gives a static checking error - [Angular] Unknown method '$any'. Any way to remove that? And if you wish you may post this as an answer and I can mark it as accepted :)

    – scharnyw
    Nov 15 '18 at 5:14











  • It looks like Angular language service doesn't support $any yet but AOT does angular.io/guide/aot-compiler#disabling-type-checking-using-any

    – yurzui
    Nov 15 '18 at 5:17











  • This is why it doesn't work in aot. stackblitz.com/edit/angular-bpgmal?file=src/app/… AOT handles $event as any.

    – yurzui
    Nov 15 '18 at 5:20











  • Another option you can try is (change)="myVariable = MyEnum['' + $event];"

    – yurzui
    Nov 15 '18 at 5:22



















  • (change)="myVariable = $any(MyEnum[$event]);"

    – yurzui
    Nov 15 '18 at 5:05











  • @yurzui This fixes it! But Angular in VS code gives a static checking error - [Angular] Unknown method '$any'. Any way to remove that? And if you wish you may post this as an answer and I can mark it as accepted :)

    – scharnyw
    Nov 15 '18 at 5:14











  • It looks like Angular language service doesn't support $any yet but AOT does angular.io/guide/aot-compiler#disabling-type-checking-using-any

    – yurzui
    Nov 15 '18 at 5:17











  • This is why it doesn't work in aot. stackblitz.com/edit/angular-bpgmal?file=src/app/… AOT handles $event as any.

    – yurzui
    Nov 15 '18 at 5:20











  • Another option you can try is (change)="myVariable = MyEnum['' + $event];"

    – yurzui
    Nov 15 '18 at 5:22

















(change)="myVariable = $any(MyEnum[$event]);"

– yurzui
Nov 15 '18 at 5:05





(change)="myVariable = $any(MyEnum[$event]);"

– yurzui
Nov 15 '18 at 5:05













@yurzui This fixes it! But Angular in VS code gives a static checking error - [Angular] Unknown method '$any'. Any way to remove that? And if you wish you may post this as an answer and I can mark it as accepted :)

– scharnyw
Nov 15 '18 at 5:14





@yurzui This fixes it! But Angular in VS code gives a static checking error - [Angular] Unknown method '$any'. Any way to remove that? And if you wish you may post this as an answer and I can mark it as accepted :)

– scharnyw
Nov 15 '18 at 5:14













It looks like Angular language service doesn't support $any yet but AOT does angular.io/guide/aot-compiler#disabling-type-checking-using-any

– yurzui
Nov 15 '18 at 5:17





It looks like Angular language service doesn't support $any yet but AOT does angular.io/guide/aot-compiler#disabling-type-checking-using-any

– yurzui
Nov 15 '18 at 5:17













This is why it doesn't work in aot. stackblitz.com/edit/angular-bpgmal?file=src/app/… AOT handles $event as any.

– yurzui
Nov 15 '18 at 5:20





This is why it doesn't work in aot. stackblitz.com/edit/angular-bpgmal?file=src/app/… AOT handles $event as any.

– yurzui
Nov 15 '18 at 5:20













Another option you can try is (change)="myVariable = MyEnum['' + $event];"

– yurzui
Nov 15 '18 at 5:22





Another option you can try is (change)="myVariable = MyEnum['' + $event];"

– yurzui
Nov 15 '18 at 5:22












2 Answers
2






active

oldest

votes


















0














Your template doesn't recognise it as an enum so you need to just assign it as a string and read it as a enum in typescript - I'm not sure that this the correct way but you can give a try



Every enum will match the values with key pars and consider passing the key and get desired output in your enum key seems to be a number and you are trying to fetch it as a string so chage it like this



enum MyEnum {
Value1 = "Value1",
Value2 = "Value2"
}


Now when your read it as MyEnum.Value1.toString() it will return you the specific value as "Value1"



Now read it like this public myVariable: string = MyEnum.Value1.toString() passing to the template should ne string property if you want to read it in ts you can pass the desired string to get the value from enum



<mat-form-field>
<mat-select placeholder="Select a value" [value]="myVariable" (valueChange)="myVariable = $event">
<mat-option value="Value1">Value 1</mat-option>
<mat-option value="Value2">Value 2</mat-option>
</mat-select>
</mat-form-field>


I hope this will help you - try to build your project in ng build --prod and check whether it works - Happy coding!!






share|improve this answer
























  • This most certainly will work. But does that mean that it's impossible to bind an enum variable directly in Angular?

    – scharnyw
    Nov 15 '18 at 4:56











  • No its not like that you can bind in template it will work for dev build - but i had a issue like you in prod build so i found a way to get it :) hope this helps you

    – Rahul
    Nov 15 '18 at 6:26











  • Thanks! yurzui found and explained a solution in the question comments. It's to do with the way Angular AOT treats template statements.

    – scharnyw
    Nov 15 '18 at 6:29



















0














Try this.



let s = 'Value1' as 'Value1';
let myEnumVariable: MyEnum = MyEnum[s];


The error you had is because Typescript considered that s had type string and not Value1. But the enum has definite keys Value1 and Value2, not any string. Hence the error.




Why would this appear in building but not development?




Probably while development you have a file watcher that compiles your TS to JS even if there are some errors. I have the same watcher in my VSCode. However the errors are still there and the building process will therefore throw an error.






share|improve this answer


























  • I'm sorry but that's not the problem. If I didn't make it clear in the question, the problem is with template statement in Angular event binding where I'm trying to set the value of an enum variable

    – scharnyw
    Nov 15 '18 at 3:36











  • Hey @scharnyw Did it help? I've updated it multiple times.

    – Nurbol Alpysbayev
    Nov 15 '18 at 3:53











  • Thanks a lot for the reply! Unfortunately Angular template statement doesn't accept Typescript "as" operator. Btw the conversion from string to enum works fine when debugging under ng serve (I've checked that the binding works), it even works with ng build (I've hosted the files generated and checked in browser), but for some reason it doesn't work with ng build --prod. Seems to me this must be some rather strange features with Angular production build/optimizations.

    – scharnyw
    Nov 15 '18 at 4:15











  • The fact that ng build --prod does not work is good because it should not pass type errors. Regarding type casting in templates maybe this will help: stackoverflow.com/questions/45964576/…

    – Nurbol Alpysbayev
    Nov 15 '18 at 4:19











  • Ok. It does seem that there is no choice but to have a method in TS for this purpose. Thanks a lot!

    – scharnyw
    Nov 15 '18 at 5:03











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%2f53311849%2fconvert-string-to-typescript-enum-in-angular-6-template-statement%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Your template doesn't recognise it as an enum so you need to just assign it as a string and read it as a enum in typescript - I'm not sure that this the correct way but you can give a try



Every enum will match the values with key pars and consider passing the key and get desired output in your enum key seems to be a number and you are trying to fetch it as a string so chage it like this



enum MyEnum {
Value1 = "Value1",
Value2 = "Value2"
}


Now when your read it as MyEnum.Value1.toString() it will return you the specific value as "Value1"



Now read it like this public myVariable: string = MyEnum.Value1.toString() passing to the template should ne string property if you want to read it in ts you can pass the desired string to get the value from enum



<mat-form-field>
<mat-select placeholder="Select a value" [value]="myVariable" (valueChange)="myVariable = $event">
<mat-option value="Value1">Value 1</mat-option>
<mat-option value="Value2">Value 2</mat-option>
</mat-select>
</mat-form-field>


I hope this will help you - try to build your project in ng build --prod and check whether it works - Happy coding!!






share|improve this answer
























  • This most certainly will work. But does that mean that it's impossible to bind an enum variable directly in Angular?

    – scharnyw
    Nov 15 '18 at 4:56











  • No its not like that you can bind in template it will work for dev build - but i had a issue like you in prod build so i found a way to get it :) hope this helps you

    – Rahul
    Nov 15 '18 at 6:26











  • Thanks! yurzui found and explained a solution in the question comments. It's to do with the way Angular AOT treats template statements.

    – scharnyw
    Nov 15 '18 at 6:29
















0














Your template doesn't recognise it as an enum so you need to just assign it as a string and read it as a enum in typescript - I'm not sure that this the correct way but you can give a try



Every enum will match the values with key pars and consider passing the key and get desired output in your enum key seems to be a number and you are trying to fetch it as a string so chage it like this



enum MyEnum {
Value1 = "Value1",
Value2 = "Value2"
}


Now when your read it as MyEnum.Value1.toString() it will return you the specific value as "Value1"



Now read it like this public myVariable: string = MyEnum.Value1.toString() passing to the template should ne string property if you want to read it in ts you can pass the desired string to get the value from enum



<mat-form-field>
<mat-select placeholder="Select a value" [value]="myVariable" (valueChange)="myVariable = $event">
<mat-option value="Value1">Value 1</mat-option>
<mat-option value="Value2">Value 2</mat-option>
</mat-select>
</mat-form-field>


I hope this will help you - try to build your project in ng build --prod and check whether it works - Happy coding!!






share|improve this answer
























  • This most certainly will work. But does that mean that it's impossible to bind an enum variable directly in Angular?

    – scharnyw
    Nov 15 '18 at 4:56











  • No its not like that you can bind in template it will work for dev build - but i had a issue like you in prod build so i found a way to get it :) hope this helps you

    – Rahul
    Nov 15 '18 at 6:26











  • Thanks! yurzui found and explained a solution in the question comments. It's to do with the way Angular AOT treats template statements.

    – scharnyw
    Nov 15 '18 at 6:29














0












0








0







Your template doesn't recognise it as an enum so you need to just assign it as a string and read it as a enum in typescript - I'm not sure that this the correct way but you can give a try



Every enum will match the values with key pars and consider passing the key and get desired output in your enum key seems to be a number and you are trying to fetch it as a string so chage it like this



enum MyEnum {
Value1 = "Value1",
Value2 = "Value2"
}


Now when your read it as MyEnum.Value1.toString() it will return you the specific value as "Value1"



Now read it like this public myVariable: string = MyEnum.Value1.toString() passing to the template should ne string property if you want to read it in ts you can pass the desired string to get the value from enum



<mat-form-field>
<mat-select placeholder="Select a value" [value]="myVariable" (valueChange)="myVariable = $event">
<mat-option value="Value1">Value 1</mat-option>
<mat-option value="Value2">Value 2</mat-option>
</mat-select>
</mat-form-field>


I hope this will help you - try to build your project in ng build --prod and check whether it works - Happy coding!!






share|improve this answer













Your template doesn't recognise it as an enum so you need to just assign it as a string and read it as a enum in typescript - I'm not sure that this the correct way but you can give a try



Every enum will match the values with key pars and consider passing the key and get desired output in your enum key seems to be a number and you are trying to fetch it as a string so chage it like this



enum MyEnum {
Value1 = "Value1",
Value2 = "Value2"
}


Now when your read it as MyEnum.Value1.toString() it will return you the specific value as "Value1"



Now read it like this public myVariable: string = MyEnum.Value1.toString() passing to the template should ne string property if you want to read it in ts you can pass the desired string to get the value from enum



<mat-form-field>
<mat-select placeholder="Select a value" [value]="myVariable" (valueChange)="myVariable = $event">
<mat-option value="Value1">Value 1</mat-option>
<mat-option value="Value2">Value 2</mat-option>
</mat-select>
</mat-form-field>


I hope this will help you - try to build your project in ng build --prod and check whether it works - Happy coding!!







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 4:15









RahulRahul

1,0331315




1,0331315













  • This most certainly will work. But does that mean that it's impossible to bind an enum variable directly in Angular?

    – scharnyw
    Nov 15 '18 at 4:56











  • No its not like that you can bind in template it will work for dev build - but i had a issue like you in prod build so i found a way to get it :) hope this helps you

    – Rahul
    Nov 15 '18 at 6:26











  • Thanks! yurzui found and explained a solution in the question comments. It's to do with the way Angular AOT treats template statements.

    – scharnyw
    Nov 15 '18 at 6:29



















  • This most certainly will work. But does that mean that it's impossible to bind an enum variable directly in Angular?

    – scharnyw
    Nov 15 '18 at 4:56











  • No its not like that you can bind in template it will work for dev build - but i had a issue like you in prod build so i found a way to get it :) hope this helps you

    – Rahul
    Nov 15 '18 at 6:26











  • Thanks! yurzui found and explained a solution in the question comments. It's to do with the way Angular AOT treats template statements.

    – scharnyw
    Nov 15 '18 at 6:29

















This most certainly will work. But does that mean that it's impossible to bind an enum variable directly in Angular?

– scharnyw
Nov 15 '18 at 4:56





This most certainly will work. But does that mean that it's impossible to bind an enum variable directly in Angular?

– scharnyw
Nov 15 '18 at 4:56













No its not like that you can bind in template it will work for dev build - but i had a issue like you in prod build so i found a way to get it :) hope this helps you

– Rahul
Nov 15 '18 at 6:26





No its not like that you can bind in template it will work for dev build - but i had a issue like you in prod build so i found a way to get it :) hope this helps you

– Rahul
Nov 15 '18 at 6:26













Thanks! yurzui found and explained a solution in the question comments. It's to do with the way Angular AOT treats template statements.

– scharnyw
Nov 15 '18 at 6:29





Thanks! yurzui found and explained a solution in the question comments. It's to do with the way Angular AOT treats template statements.

– scharnyw
Nov 15 '18 at 6:29













0














Try this.



let s = 'Value1' as 'Value1';
let myEnumVariable: MyEnum = MyEnum[s];


The error you had is because Typescript considered that s had type string and not Value1. But the enum has definite keys Value1 and Value2, not any string. Hence the error.




Why would this appear in building but not development?




Probably while development you have a file watcher that compiles your TS to JS even if there are some errors. I have the same watcher in my VSCode. However the errors are still there and the building process will therefore throw an error.






share|improve this answer


























  • I'm sorry but that's not the problem. If I didn't make it clear in the question, the problem is with template statement in Angular event binding where I'm trying to set the value of an enum variable

    – scharnyw
    Nov 15 '18 at 3:36











  • Hey @scharnyw Did it help? I've updated it multiple times.

    – Nurbol Alpysbayev
    Nov 15 '18 at 3:53











  • Thanks a lot for the reply! Unfortunately Angular template statement doesn't accept Typescript "as" operator. Btw the conversion from string to enum works fine when debugging under ng serve (I've checked that the binding works), it even works with ng build (I've hosted the files generated and checked in browser), but for some reason it doesn't work with ng build --prod. Seems to me this must be some rather strange features with Angular production build/optimizations.

    – scharnyw
    Nov 15 '18 at 4:15











  • The fact that ng build --prod does not work is good because it should not pass type errors. Regarding type casting in templates maybe this will help: stackoverflow.com/questions/45964576/…

    – Nurbol Alpysbayev
    Nov 15 '18 at 4:19











  • Ok. It does seem that there is no choice but to have a method in TS for this purpose. Thanks a lot!

    – scharnyw
    Nov 15 '18 at 5:03
















0














Try this.



let s = 'Value1' as 'Value1';
let myEnumVariable: MyEnum = MyEnum[s];


The error you had is because Typescript considered that s had type string and not Value1. But the enum has definite keys Value1 and Value2, not any string. Hence the error.




Why would this appear in building but not development?




Probably while development you have a file watcher that compiles your TS to JS even if there are some errors. I have the same watcher in my VSCode. However the errors are still there and the building process will therefore throw an error.






share|improve this answer


























  • I'm sorry but that's not the problem. If I didn't make it clear in the question, the problem is with template statement in Angular event binding where I'm trying to set the value of an enum variable

    – scharnyw
    Nov 15 '18 at 3:36











  • Hey @scharnyw Did it help? I've updated it multiple times.

    – Nurbol Alpysbayev
    Nov 15 '18 at 3:53











  • Thanks a lot for the reply! Unfortunately Angular template statement doesn't accept Typescript "as" operator. Btw the conversion from string to enum works fine when debugging under ng serve (I've checked that the binding works), it even works with ng build (I've hosted the files generated and checked in browser), but for some reason it doesn't work with ng build --prod. Seems to me this must be some rather strange features with Angular production build/optimizations.

    – scharnyw
    Nov 15 '18 at 4:15











  • The fact that ng build --prod does not work is good because it should not pass type errors. Regarding type casting in templates maybe this will help: stackoverflow.com/questions/45964576/…

    – Nurbol Alpysbayev
    Nov 15 '18 at 4:19











  • Ok. It does seem that there is no choice but to have a method in TS for this purpose. Thanks a lot!

    – scharnyw
    Nov 15 '18 at 5:03














0












0








0







Try this.



let s = 'Value1' as 'Value1';
let myEnumVariable: MyEnum = MyEnum[s];


The error you had is because Typescript considered that s had type string and not Value1. But the enum has definite keys Value1 and Value2, not any string. Hence the error.




Why would this appear in building but not development?




Probably while development you have a file watcher that compiles your TS to JS even if there are some errors. I have the same watcher in my VSCode. However the errors are still there and the building process will therefore throw an error.






share|improve this answer















Try this.



let s = 'Value1' as 'Value1';
let myEnumVariable: MyEnum = MyEnum[s];


The error you had is because Typescript considered that s had type string and not Value1. But the enum has definite keys Value1 and Value2, not any string. Hence the error.




Why would this appear in building but not development?




Probably while development you have a file watcher that compiles your TS to JS even if there are some errors. I have the same watcher in my VSCode. However the errors are still there and the building process will therefore throw an error.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 4:19

























answered Nov 15 '18 at 3:30









Nurbol AlpysbayevNurbol Alpysbayev

3,9621227




3,9621227













  • I'm sorry but that's not the problem. If I didn't make it clear in the question, the problem is with template statement in Angular event binding where I'm trying to set the value of an enum variable

    – scharnyw
    Nov 15 '18 at 3:36











  • Hey @scharnyw Did it help? I've updated it multiple times.

    – Nurbol Alpysbayev
    Nov 15 '18 at 3:53











  • Thanks a lot for the reply! Unfortunately Angular template statement doesn't accept Typescript "as" operator. Btw the conversion from string to enum works fine when debugging under ng serve (I've checked that the binding works), it even works with ng build (I've hosted the files generated and checked in browser), but for some reason it doesn't work with ng build --prod. Seems to me this must be some rather strange features with Angular production build/optimizations.

    – scharnyw
    Nov 15 '18 at 4:15











  • The fact that ng build --prod does not work is good because it should not pass type errors. Regarding type casting in templates maybe this will help: stackoverflow.com/questions/45964576/…

    – Nurbol Alpysbayev
    Nov 15 '18 at 4:19











  • Ok. It does seem that there is no choice but to have a method in TS for this purpose. Thanks a lot!

    – scharnyw
    Nov 15 '18 at 5:03



















  • I'm sorry but that's not the problem. If I didn't make it clear in the question, the problem is with template statement in Angular event binding where I'm trying to set the value of an enum variable

    – scharnyw
    Nov 15 '18 at 3:36











  • Hey @scharnyw Did it help? I've updated it multiple times.

    – Nurbol Alpysbayev
    Nov 15 '18 at 3:53











  • Thanks a lot for the reply! Unfortunately Angular template statement doesn't accept Typescript "as" operator. Btw the conversion from string to enum works fine when debugging under ng serve (I've checked that the binding works), it even works with ng build (I've hosted the files generated and checked in browser), but for some reason it doesn't work with ng build --prod. Seems to me this must be some rather strange features with Angular production build/optimizations.

    – scharnyw
    Nov 15 '18 at 4:15











  • The fact that ng build --prod does not work is good because it should not pass type errors. Regarding type casting in templates maybe this will help: stackoverflow.com/questions/45964576/…

    – Nurbol Alpysbayev
    Nov 15 '18 at 4:19











  • Ok. It does seem that there is no choice but to have a method in TS for this purpose. Thanks a lot!

    – scharnyw
    Nov 15 '18 at 5:03

















I'm sorry but that's not the problem. If I didn't make it clear in the question, the problem is with template statement in Angular event binding where I'm trying to set the value of an enum variable

– scharnyw
Nov 15 '18 at 3:36





I'm sorry but that's not the problem. If I didn't make it clear in the question, the problem is with template statement in Angular event binding where I'm trying to set the value of an enum variable

– scharnyw
Nov 15 '18 at 3:36













Hey @scharnyw Did it help? I've updated it multiple times.

– Nurbol Alpysbayev
Nov 15 '18 at 3:53





Hey @scharnyw Did it help? I've updated it multiple times.

– Nurbol Alpysbayev
Nov 15 '18 at 3:53













Thanks a lot for the reply! Unfortunately Angular template statement doesn't accept Typescript "as" operator. Btw the conversion from string to enum works fine when debugging under ng serve (I've checked that the binding works), it even works with ng build (I've hosted the files generated and checked in browser), but for some reason it doesn't work with ng build --prod. Seems to me this must be some rather strange features with Angular production build/optimizations.

– scharnyw
Nov 15 '18 at 4:15





Thanks a lot for the reply! Unfortunately Angular template statement doesn't accept Typescript "as" operator. Btw the conversion from string to enum works fine when debugging under ng serve (I've checked that the binding works), it even works with ng build (I've hosted the files generated and checked in browser), but for some reason it doesn't work with ng build --prod. Seems to me this must be some rather strange features with Angular production build/optimizations.

– scharnyw
Nov 15 '18 at 4:15













The fact that ng build --prod does not work is good because it should not pass type errors. Regarding type casting in templates maybe this will help: stackoverflow.com/questions/45964576/…

– Nurbol Alpysbayev
Nov 15 '18 at 4:19





The fact that ng build --prod does not work is good because it should not pass type errors. Regarding type casting in templates maybe this will help: stackoverflow.com/questions/45964576/…

– Nurbol Alpysbayev
Nov 15 '18 at 4:19













Ok. It does seem that there is no choice but to have a method in TS for this purpose. Thanks a lot!

– scharnyw
Nov 15 '18 at 5:03





Ok. It does seem that there is no choice but to have a method in TS for this purpose. Thanks a lot!

– scharnyw
Nov 15 '18 at 5:03


















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%2f53311849%2fconvert-string-to-typescript-enum-in-angular-6-template-statement%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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini