title: TypeScript types serialisation/deserialization in localstorage
body: <p>I have a Typescript app. I use the localstorage for development purpose to store my objects and I have the problem at the deserialization.</p>
  
  <p>I have an object meeting of type MeetingModel:</p>
  
  <pre><code>export interface MeetingModel {
      date: moment.Moment; // from the library momentjs
  }
  </code></pre>
  
  <p>I store this object in the localStorage using <code>JSON.stringify(meeting)</code>.</p>
  
  <p>I suppose that stringify call moment.toJson(), that returns an iso string, hence the value stored is: <code>{"date":"2016-12-26T15:03:54.586Z"}</code>. </p>
  
  <p>When I retrieve this object, I do:</p>
  
  <pre><code>const stored = window.localStorage.getItem("meeting");
  const meeting: MeetingModel = JSON.parse(stored);
  </code></pre>
  
  <p>The problem is: meeting.date contains a string instead of a moment !</p>
  
  <p>So, first I'm wondering why TypeScript let this happen ? Why can I assign a string value instead of a Moment and the compiler agree ?</p>
  
  <p>Second, how can I restore my objects from plain JSON objects (aka strings) into Typescript types ?</p>
  
  <p>I can create a factory of course, but when my object database will grow up it will be a pain in the *** to do all this work.</p>
  
  <p>Maybe there is a solution for better storing in the local storage in the first place?</p>
  
  <p>Thank you</p>