Classes in TypeScript

  • Classes in TypeScript is similar to that of most of the classes in other languages.
  • Classes are implemented in JavaScript as methods in the compiled .js code.
  • Classes have Constructors and new key word to declare an Object in TypeScript.

Example of Simple Class, data members and type creation in TypeScript

Methods in Classes

  • Just like we data members, we have member functions as well in TypeScript.
  • We can define data type of parameters and return type in these methods.


  • Constructors in TypeScript can be create using keyword constructor()
  • We can only have one constructor in TypeScript
  • Constructors can be parameterized as well as empty.
class Person{
    private firstName : String;
    private lastName : String;
    public age : number;
        this.firstName = "";
        this.lastName = "";
    public setName(firstName : String, lastName : String){
        this.firstName = firstName;
        this.lastName = lastName;
var aPerson : Person= new Person(); //Declaring a variable of Type Person
aPerson.age = 70;

After compilation the above code is converted into JavaScript and it looks like this :

Leave a Comment