Type Script Class | Class Example in Typescript

What is Class:-

  • “Class” represents a model of the object, which defines list of properties and methods of the object.
  • Ex: “Student” class represents structure (list of properties and methods) of every student object.
  • For example, studentname=”Suresh” is a property in the “student object”.
  • We can create any no. of objects based on a class, using “new” keyword.
  • All the objects of a class, shares same set of properties & methods of the class.
  • We can store the reference of the object in “reference variable”; using which you can access the object.
  • Create a Class:
  • Create an Object & Store its address in Reference Variable var referencevariablename = new classname( );
  • Access Properties and Methods using Reference Variable referencevariablename.property referencevariablename.method( );
  • Constructor is a special function which is a part of the class.
  • Constructor will be called automatically when we create a new object for the class. If you create multiple objects for the same class, the constructor will be called each time when you create new object.
  • Constructor is used to initialize properties of the class.
  • Constructor’s name should be “constructor”.
  • Constructor can receive arguments; but can’t return any value.
  • In TypeScript, we can’t define multiple constructors.
  • Example: