I have a json file which looks like these:
{
"ui": "Header"
}
while "ui" is an enum. I load this part with JSON.Parse in my typescript webpack envoriment with node.js and ts-loader, so my error is:
"Type 'string' is not assignable to type 'UiDescriptionTypeEnum'."
and my interface I am trying to cast into is:
interface UI
{
ui: UiDescriptionTypeEnum
}
while having the enum like this:
enum UiDescriptionTypeEnum
{
Header = "Header"
}
Here is a working typescript play with the error:
Values in the enum are case sensitive
try to redefine the enum as below
enum UiDescriptionTypeEnum
{
header = "header"
}
Updated the example from the link
enum MyEnum
{
Header = "Header"
}
interface MyInterface
{
test: MyEnum
}
var json = { test: 'Header' }
var jsonObj = {test: MyEnum.Header} // option 1: to define the type from Enum
let a : MyInterface = json as MyInterface; // option 2: cast js object to your interface ( a more likely scenario)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.