How to Use Super Keyword In TypeScript

super keyword in TypeScript

 
The super keyword can be used in expressions to reference base class properties and the base class constructor. Super calls consist of the keyword super followed by an argument list enclosed in parentheses. Super calls are only permitted in constructors of derived classes.
 

super call

 
A super call invokes the constructor of the base class on the instance referenced by this. A super call is processed as a function call using the construct signatures of the base class constructor function type as the initial set of candidate signatures for overload resolution.
 
The type of a super call expression is Void.
 

super property

 
A super property access consists of the keyword super followed by a dot and an identifier. Super property accesses are used to access base class instance member functions from derived classes. A super property access is permitted only in a constructor, instance member function, or instance member accessor of a derived class and must specify a public instance member function of the base class. It is not possible to access other kinds of base class members in a super property access.
 
Super property accesses are typically used to access overridden base class instance member functions from derived class instance member functions.
 
In the following example, we are passing the name to the base class constructor from the derived class constructor. Also, note that we are using a parameter property declaration to declare properties with the same name and value as constructor parameters.
 

TypeScript application 

 
Let's use the following steps.
 
Step 1. Open Visual Studio 2012 and click "File" -> "New" -> "Project...". A window is shown as:
 
start-super-keyword-type-script.gif
 
Give the name of your application as "super" and then click ok.
 
Step 2. After this session the project has been created; your new project should look like this:
 
super-explorer-type-script.gif
 

Coding

 
super.ts 
  1. class person {  
  2.  constructor(public name: string) {}  
  3. }  
  4. class intro extends person {  
  5.  constructor(public name: string, public age: number) {  
  6.   super(name);  
  7.  }  
  8.  sayHello() {  
  9.   var span = document.createElement("span");  
  10.   span.innerText = "Hello, I'm " + this.name + " with age " + this.age;  
  11.   document.body.appendChild(span);  
  12.  }  
  13. }  
  14. window.onload = () => {  
  15.  var d: intro = new intro("Nitin", 23);  
  16.  d.sayHello();  
  17. }   
superkeyworddemo.html
  1. < !DOCTYPEhtml >  
  2.  <  
  3.  htmllanghtmllang = "en"  
  4. xmlns = "http://www.w3.org/1999/xhtml" >  
  5.  <  
  6.  head >  
  7.  <  
  8.  metacharsetmetacharset = "utf-8" / >  
  9.  <  
  10.  title > TypeScript HTML App < /title>   <  
  11.  linkrellinkrel = "stylesheet"  
  12. href = "app.css"  
  13. type = "text/css" / >  
  14.  <  
  15.  scriptsrcscriptsrc = "app.js" > < /script>   <  
  16.  /head>   <  
  17.  body >  
  18.  <  
  19.  h3 > "Super Keyword"  
  20. Example in TypeScript HTML App < /h3>   <  
  21.  dividdivid = "content" / >  
  22.  <  
  23.  /body>   <  
  24.  /html>   
app.js
  1. var __extends = this.__extends || function(d, b) {  
  2.  function __() {  
  3.   this.constructor = d;  
  4.  }  
  5.  __.prototype = b.prototype;  
  6.  d.prototype = new __();  
  7. }  
  8. var person = (function() {  
  9.  function person(name) {  
  10.   this.name = name;  
  11.  }  
  12.  return person;  
  13. })();  
  14. var intro = (function(_super) {  
  15.  __extends(intro, _super);  
  16.   
  17.  function intro(name, age) {  
  18.   _super.call(this, name);  
  19.   this.name = name;  
  20.   this.age = age;  
  21.  }  
  22.  intro.prototype.sayHello = function() {  
  23.   var span = document.createElement("span");  
  24.   span.innerText = "Hello, I'm " + this.name + " with age " + this.age;  
  25.   document.body.appendChild(span);  
  26.  };  
  27.  return intro;  
  28. })(person);  
  29. window.onload = function() {  
  30.  var d = new intro("Nitin", 23);  
  31.  d.sayHello();  
  32. };   
Output
 
super-keyword-type-script.gif
 

Summary

 
In this article, we learned how to use the TypeScript super keyword.


Similar Articles