title: How to convert typescript class to plain javascript object with class-transformer
body: <p>I've some typescript classes instances in my angular app that I've to save to firebase. Firebase isn't supporting custom classes, so I found this: <a href="https://github.com/typestack/class-transformer" rel="nofollow noreferrer">https://github.com/typestack/class-transformer</a> that seems well fitted for me.</p>
  <p>So now I'm creating my object, and trying to convert it.</p>
  <p>Here is my object.</p>
  <pre><code>export class Trip {
    id: string;
    owner: string;
    name: string;
    @Type(() =&gt; Date)
    startDate: Date;
    @Type(() =&gt; Date)
    endDate: Date | undefined;
    coverImageUrl: string;
  
    @Type(() =&gt; Itinerary)
    itinerary: Itinerary;
  
    constructor() {
      this.itinerary = new Itinerary();
    }
  }
  
  export class Itinerary {
    @Type(() =&gt; ItineraryStats)
    stats: ItineraryStats;
    @Type(() =&gt; Step, {
      discriminator: {
        property: '__type',
        subTypes: [
          { value: ActivityStep, name: 'ActivityStep' },
          { value: NightStep, name: 'NightStep' },
          { value: PointOfInterest, name: 'PointOfInterest' },
          { value: TravelStep, name: 'TravelStep' },
        ],
      },
    })
    steps: Step[];
  
    constructor() {
      this.steps = Step[0];
      this.stats = new ItineraryStats();
    }
  }
  
  export class ItineraryStats {
    ///In days
    duration: number;
    //in hours
    driveTime: number;
    //in kilometers
    driveDistance: number;
  
    averageDriveDistancePerDay(): number {
      return this.driveDistance / this.duration;
    }
    averageDriveTimePerDay(): number {
      return this.driveTime / this.duration;
    }
  }
  
  
  export abstract class Step {
    @Type(() =&gt; Date)
    start: Date;
  
    @Type(() =&gt; Date)
    end: Date;
    duration: number;
    enforcedStartStop: boolean;
    warning: string;
  }
  
  export class ActivityStep extends StopStep {
    duration: number;
  
    constructor() {
      super();
      this.id = Guid.newGuid();
    }
  }
  
  
  export class NightStep extends StopStep {
    nightNumber: number;
    numberOfNight: number;
  }
  
  export class PointOfInterest {
    @Type(()=&gt;PointOfInterest)
    type: PointOfInterest;
  }
  
  
  export class TravelStep extends Step {
    @Type(() =&gt; TravelMode)
    Mode: TravelMode;
    PolyLines: string;
  }
  </code></pre>
  <p>I'm creating one &quot;trip&quot; and trying to convert it:</p>
  <pre><code>const trip = new Trip();
  trip.owner= firebase.auth().currentUser.uid;
  trip.name= action.name;
  trip.startDate = action.startDate,
  trip.endDate = action.endDate;
  console.log(trip);
  const converted = classToPlain(trip)
  </code></pre>
  <p>But I get this:</p>
  <pre><code>ERROR TypeError: Cannot read property 'constructor' of undefined
      at TransformOperationExecutor.js:207
      at Array.find (&lt;anonymous&gt;)
      at TransformOperationExecutor.transform (TransformOperationExecutor.js:207)
      at TransformOperationExecutor.transform (TransformOperationExecutor.js:266)
      at ClassTransformer.classToPlain (ClassTransformer.js:10)
      at classToPlain (index.js:8)
      at Object.toFirestore (trips.firestore.ts:16)
      at ma (index.cjs.js:15983)
      at n.set (index.cjs.js:15359)
      at AngularFirestoreDocument.set (angular-fire-firestore.js:512)
  </code></pre>
  <p>The object I just created:
  <a href="https://i.stack.imgur.com/GG3FC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GG3FC.png" alt="enter image description here" /></a></p>
  <p>I guess either my object is in an improper state or I've something poorly defined, but I can't find what?</p>