0

I receive some JSON which can be a TextField object or a DateField object. Both inherit the Field superclass. The JSON I receive is converted to a Field object. I then need to cast this Field object into the corresponding type. To do this, I have an Id inside the JSON which lets me know if it's a TextField or DateField object. Everything works if I create a new TextField object and assign him the values of the JSON manually. However, I'd like to cast it directly to the corresponding class as it has the same attributes as the JSON. Something like newField = <TextareaField>field; but then, if I do newField instanceof TextField, it doesn't work. Any idea on how to cast an object to another type?

Elbbard
  • 2,064
  • 6
  • 30
  • 53

1 Answers1

-2

It seems to work if I do:

newField = Object.assign(new TextField(), field);
Elbbard
  • 2,064
  • 6
  • 30
  • 53
  • I know this is not efficient but could you give me another way of doing it instead of just downvoting? – Elbbard Mar 31 '17 at 11:46