Search code examples
angulartypescriptangular7angular8

How to Reset clear the values in a Typescript class


How do I reset the values in a class in Typescript and ensure all the values are cleared? Is there a typescript function to do this?

export class Product{
   productId: number; 
   productName: string;
   productDescription: string;
}

We have many classes with 50+ fields, looking for efficient way to conduct this.


Solution

  • Try like this:

    Working Demo

    productDetails: Product = {
      productId: 1,
      productName: "Apple",
      productDescription: "Fruit"
    };
    
    reset() {
      this.productDetails = new Product();
    }