1

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:

https://www.typescriptlang.org/play?#code/KYOwrgtgBAsgngUXBAsAKAN7qjqAJYAQwBNgAnKAXigCICTyb0BfddASxABdyAzQgMbBYcAJLc+g4eixpcUHgGcuALhFJILNmgBuhCgCtFAexBUoGBcGVqA5PVJlbUVmnQAbYFyiE18cTxk-ELmRqYA3EA

PassionateDeveloper
  • 14,558
  • 34
  • 107
  • 176
  • Does this answer your question? [How do I convert a string to enum in TypeScript?](https://stackoverflow.com/questions/17380845/how-do-i-convert-a-string-to-enum-in-typescript) – rmjoia Dec 30 '20 at 09:01
  • Thank you, it did not. I read into it, but my main problem seems the json converter here, not typescript itself – PassionateDeveloper Dec 30 '20 at 09:04
  • is it a case issue like Caroline pointed out? – rmjoia Dec 30 '20 at 09:06
  • AI allready answered that: no it isnt. – PassionateDeveloper Dec 30 '20 at 09:08
  • missed that, sorry. check out this one https://stackoverflow.com/questions/35760096/typescript-enum-from-json-string Enum in Json in Typescript with Interface? – rmjoia Dec 30 '20 at 09:09
  • I read this 10 times, but as I understand my syntax would be completly fine – PassionateDeveloper Dec 30 '20 at 09:10
  • @rmjoia added a playground linkl – PassionateDeveloper Dec 30 '20 at 09:15
  • I replaced your line 13 with `let a: MyInterface = JSON.parse(JSON.stringify(json))` and it works.. `[LOG]: { "test": "Header" } ` – rmjoia Dec 30 '20 at 09:18
  • btw.. `let a: MyInterface = json as MyInterface` also works https://www.typescriptlang.org/play?#code/KYOwrgtgBAsgngUXBAsAKAN7qjqAJYAQwBNgAnKAXigCICTyb0BfddASxABdyAzQgMbBYcAJLc+g4eixpcUHgGcuALhFJILNmgBuhCgCtFAexBUoGBcGVqA5PVJlbUVmnQAbYFyiE18cTxk-ELmlkqqUPZEjs7MPooiAZJC2p7eAEZ+YhJBUuZGpvGJOcHA2gKmJp4AdO7GAOYAFIQAlOgVIFXAtQ2N6S1AA – rmjoia Dec 30 '20 at 09:20
  • Well it works fine for me too, but why I have to convert it? – PassionateDeveloper Dec 30 '20 at 09:22
  • I suppose that, if you don't parse the json, it's just an object, and Typescript infers it as an object, on the other example, you cast that object to your type, and then Typescript knows what you want. – rmjoia Dec 30 '20 at 09:25

1 Answers1

1

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)
Caroline D.
  • 119
  • 3