Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.


2 Answers

Try like this:

Working Demo

productDetails: Product = {
  productId: 1,
  productName: "Apple",
  productDescription: "Fruit"
};

reset() {
  this.productDetails = new Product();
}
like image 179
Adrita Sharma Avatar answered Nov 19 '25 02:11

Adrita Sharma


YOu can create a function and set your all states to default value

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

  resetValues() {
    this.productId = 0; //default of number datatype is 0
    this.productName = null;
    this.productDescription = null;
  }
}
like image 24
Saurabh Yadav Avatar answered Nov 19 '25 01:11

Saurabh Yadav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!