By 06/07/2015 13 Comments

The Benefits Of TypeScript

TypeScript Programming Language Has The Following Benefits:

  • Strong typing.
  • Compilation.
  • Member variables with public and private access.
  • Encapsulation.
  • Integration with JavaScript libraries.

The Strong Typing:

  • JavaScript language is not a strong type language it is a weak by nature.
  • Its data has not any specific type.
  • In JavaScript data types are declared using a keyword “var” and we can assign any type of values in that variables. No any syntax error will be returned by JavaScript.
  • TypeScript allows us to write JavaScript code in a strong environment through visual studio IDE which provides intellisense or autocomplete feature.
  • TypeScript compiler provides strong or static type checking to JavaScript code and give us full benefits of a strong typed language. It checks errors at compile time.
  • In strong type environment we can define the different types of data such as int, string etc.

Example:

class Technology { // TypeScript class declaration

/* following is the TypeScript strong typed properties/variable initialization which has string and boolean types. */
    technologyName: string = "TypeScript";
    isStrongTyped: boolean = true;

/* following is 2 function/method declaration which print different information through document’s built-in method which uses an id ('content') for display purpose. */
    displayTechnology() {
       document.getElementById('content').innerHTML = "Technology is: " + this.technologyName;
    }

    strongType() {
        document.getElementById('content').innerHTML += "<br/>Is TypeScript strong typed? " + this.isStrongTyped;
    }
}

// following is JavaScript window object’s event (onload) fired.
window.onload = () => {

// An object creation of above class.
    var technology = new Technology();

// Methods are accessed/called through an object reference.
    technology.displayTechnology();
    technology.strongType();
};

The Compilation:

  • By default the JavaScript programing language has not a well-structured compilation process/step because it is an interpreted weakly typed client side language.
  • JavaScript programming language has not debugging feature therefore its code is harder to debug/error check because it has not any specific compiler.
  • TypeScript compiles our JavaScript code and generates compilation errors if it finds any syntax error in our source code before running our program.
  • Using TypeScript tool/language we can easily compile/debug and find errors in our JavaScript applications quickly without time wasting.

Member Variables/Properties With Public And Private Access:

  • JavaScript has not public and private access modifiers. The JavaScript programmers make variables private by using an underscore (_) sign with variable names such as _number = 0.
  • TypeScript supports encapsulation feature which provides the data hiding concepts. Data hiding concept has two keywords “public” and “private”.
  • In TypeScript “public” and “private” modifiers is used on member variables inside the classes.
  • In TypeScript if we try to access any “private” member variable outside the class the TypeScript compiler generates a compile time error because any “private” member variable cannot be accessed/called directly from outside its class. A “private” member variable is accessible within its class only. A “private” modifier hides the member variable. A “private” member variable can be accessed via other members of a class.
  • All member variables in TypeScript have their “public” access by default.
  • A member variable with “public” access can be accessed freely anywhere in the code. It is also called outside its class.
  • In TypeScript the “public” and “private” access modifiers are compile-time features only and do not effect on generated JavaScript.
  • The TypeScript compiler still generates the JavaScript output file, even if there are compile-time errors.

Example:

class Technology { // TypeScript class declaration

/* following is the TypeScript strong typed properties/variable initialization which has string type. Notice 1 variable has private access and other has public. */
    private javaScript: string = "JavaScript";
    public TypeScript: string = "TypeScript";

// method declaration which prints a private variable.
    displayJs() {
        document.write("Technology 1 is: " + this.javaScript);
    }
}

// onload event is fired.
window.onload = () => {

// above clas object is created.
    var technology = new Technology();

/* displayJs() method is called through an object reference. This method prints a private variable. */
    technology.displayJs();

/* following print a public variable (TypeScript) directly. Remember a variable with private keyword (javaScript) cannot be called/accessed directly because it has a private access. */
    document.write("<br/>Technology 2 is: "+technology.TypeScript);
};

Encapsulation:

  • Encapsulation is one of a fundamental principal of Object-oriented programming.
  • Through encapsulation the attributes and behaviors (data members and functions/methods) is defined inside a class. Collectively they both (data variables and functions/methods) are called class members.
  • Methods/functions operates data members.
  • In TypeScript we can use fully functional Object-oriented asses and its principals such as (Encapsulation).
  • JavaScript has not a keyword “class”. It implements encapsulation by either using its prototype pattern or by using the closure pattern.
  • TypeScript compiler generates the JavaScript closure from the class definition.

Example:

class Calculator { // Class declaration

// number type private fields initialization. 
    private v1: number = 10;
    private v2: number = 2;

/* following is 4 calculation operation type methods which returns values depends on their operation type. Values are returned via a keyword “return”. */
    addition() {
        return (this.v1 + this.v2);
    }

    substraction() {
        return (this.v1 - this.v2);
    }

    multiplication() {
        return (this.v1 * this.v2);
    }

    division() {
        return (this.v1 / this.v2);
    }

}

// onload event fired.
window.onload = () => {

// calculation object creation
  var calculator = new Calculator();

/* access the above methods through calculator object reference and prints them. */
  document.write("Addition: " + calculator.addition() + " <br/>");
  document.write("Substraction: " + calculator.substraction() + " <br/>");
  document.write("Multiplication: " + calculator.multiplication() + " <br/>");
  document.write("Division: " + calculator.division());
};

 Integration With JavaScript Libraries:

  • Any JavaScript library can be integrated with TypeScript.
  • A JavaScript library can be integrated by creating a definition header file. That definition header file can be used by TypeScript and it has .d.ts extension such as (file-name.d.ts). TypeScript uses header file much like as C++ uses.
  • These definition files contain the information which describes each available functions and variables of the library, along with their associated type annotations/function signature.
  • Visual studio IDE gives us great intellisense support while writing the programs in TypeScript. Actually these methods in intellisense is loaded from lib.d.ts file which is a definition library file and contains JavaScript’s core built-in methods in strong type format written in TypeScript syntax. It is a built-in library in TypeScript.
  • Some Definition library files can be downloaded from https://github.com/borisyankov/DefinitelyTyped along with TypeScript files.

 







Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .