|Language:||English, Spanish, Indonesian|
|Distribution:||Free* [*Registration Required]|
TypeScript Functions TypeScript functions can have typed arguments and a return type. Return type of the function is a number, so the variable numResult also gets the type number. It is possible to make the function accept either a number or a string using union types.
We need to define a union type to represent number or string and use it as the type in the function definition. If both types of both parameters are different, it returns a null. Out of the three calls made to the function, we get result in the first two instances and the third one returns null. TypeScript Classes Classes are used to define blueprint of objects.
They are used extensively in OOPs based languages. TypeScript had support for classes since its inception. Classes in TypeScript can have properties and members as instance variables.
Their access to the outside world can be controlled using access specifiers. The class can have a constructor to initialize its members.
Arguments of the constructor can be automatically converted to instance members if an access specifier is specified in the argument.
The function getDetails returns a string containing details of the employee. The default access specifier in TypeScript is public. Here, the method getDetails becomes public. It can be called using any object of the class. The property Name with public getter and setter blocks are encapsulating the private field name in the class. This is to prevent direct access to the instance member from outside of the class.
And the field bonus just has a getter block around it which allows it to be accessed from outside the class and prevent assigning any value to it.
We can instantiate a class using the new keyword. Public members of the class can be accessed using the object created. Bonus ; Inheritance in TypeScript Reusability is a word that we cannot ignore as programmers.
We come across this word every now and then. One of the several ways in which code reusability can be achieved while dealing with classes and objects is through inheritance. A class can be inherited from an existing class. The new class gets members of the parent class along with its own. The child class gets access to all protected and public members of the parent class and not to the private members.
When a child class is instantiated, the parent class also gets instantiated. The child class can call constructor of the parent class using the super keyword.
The super keyword in the child class refers to the current instance of the parent class. Call to the parent class constructor has to be the first statement in the child class.
If the parent class has a parameterized constructor, it is mandatory to call the parent class constructor. Also, observe the way the getDetails method calls the parent class method.
This method overrides the parent class method. When getDetails is invoked using an object of the Manager class, it calls method of the Manager class. This completely hides the parent class method. But the parent class method is available for the child class to invoke, and it can be done using the super keyword. Interfaces in TypeScript Interfaces are used to create contracts. Because of this, an interface cannot be used as-is to build anything. An interface is meant to be inherited by a class and the class implementing the interface has to define all members of the interface.