2

I'm new to ts and I'm trying to deserialize a json to a statically typed object

like this

class Bar {
    x:number;
    y?:string; 
 }
  
interface IBar{
    x:number;
    y?:string; 
}

var baz:IBar = JSON.parse(jsonString);

i want to know in this case what the difference between using

var baz:IBar = JSON.parse(jsonString); 

and

var baz:Bar = JSON.parse(jsonString);

i have read Interfaces vs Types in TypeScript but it did not help

CSharp-n
  • 291
  • 2
  • 15
  • 2
    Take a look at this answer https://stackoverflow.com/a/55505227/10691359, it will help you choose between those two choices. – Pilpo Sep 15 '21 at 15:55
  • Saying it is "that class" may work in some ways, but it won't be `instanceof` nor would it have any functions on the class. Either use the interface or create a parse function for your class, or object assign the data into an instance. – crashmstr Sep 15 '21 at 16:06

0 Answers0