TypeScript
TypeScript is a pure object-oriented programming language that is open-source. It serves as a strongly typed superset of JavaScript, which compiles down to standard JavaScript. This language is developed and supported by Microsoft and operates under the Apache 2 license. TypeScript cannot be executed directly in a browser; it requires a compiler to transform it into a JavaScript file. The source files for TypeScript utilize the ".ts" extension. Any valid ".js" file can be converted to a TypeScript file simply by changing its extension to ".ts". TypeScript represents the ES6 iteration of JavaScript, incorporating several additional features.
Features of TypeScript
Object-Oriented Programming: TypeScript is structured to accommodate object-oriented programming principles, including classes, inheritance, encapsulation, interfaces, and polymorphism. These OOP concepts contribute to the development of resilient and easily maintainable software.
Transpiling to JavaScript: Since browsers lack native support for TypeScript but do support JavaScript, the initial step involves converting the TypeScript source code into JavaScript, enabling browsers to interpret the code.
Static Typing: This is a key characteristic of TypeScript, where the type of a variable is determined during compile time instead of at runtime. This implies that once a variable's data type is assigned, it cannot be altered subsequently.
Modularity: It facilitates modularity, signifying the utilization of modules that assist in structuring extensive codebases. Within these modules, substantial code is compartmentalized into smaller segments. These modules can be reused by importing and exporting them as necessary.
JavaScript Library Compatibility: TypeScript extends JavaScript, meaning that all current JS code and its libraries are also compatible with TS. This compatibility enables developers to utilize JavaScript libraries as needed.
JavaScript
JavaScript stands as the leading programming language for HTML and the Web. It is a lightweight, object-oriented scripting language that operates across multiple platforms. This language is utilized for developing dynamic client-side pages. Programs written in JavaScript are referred to as scripts. These scripts are embedded within HTML pages and are executed automatically upon page loading. They are delivered and run as plain text, requiring no special compilation or preparation prior to execution.
Features of TypeScript
Object-Oriented Programming: JavaScript functions as an object-oriented programming language that facilitates the development of adaptable and easily maintainable code. It encompasses OOP principles such as inheritance, classes, data abstraction, and more.
Lightweight Scripting Language: This refers to a scripting language that is light in weight, indicating it does not require intricate compilation processes since it is executed directly by web browsers. As a result, JS becomes a faster and more adaptable programming language.
Dynamic Typing: This is a key characteristic of JavaScript that determines the data type of a variable at runtime, allowing it to be modified as needed. There is no requirement to explicitly declare the data type of the variable.
Interpreter-Driven: This approach employs an interpreter to execute code, signifying that JS code is processed sequentially, enabling us to observe the results of JS code immediately. Consequently, most errors can be readily detected during the interpretation phase. Each web browser includes a built-in interpreter that processes JavaScript code, enhancing the interactivity and usability of web pages.
Case Sensitivity: The nature of case sensitivity implies that function names, variable names, and other identifiers must be accurately formatted. For instance, the variable names "myVar" and "myvar" represent two distinct identifiers.
TypeScript Vs. JavaScript
| SN | JavaScript | TypeScript |
|---|---|---|
1. |
It doesn't support strongly typed or static typing. | It supports strongly typed or static typing features. |
2. |
Netscape developed it in 1995. | Anders Hejlsberg developed it in 2012. |
3. |
JavaScript source file is in ".js" extension. | TypeScript source file is in ".ts" extension. |
4. |
It is directly run on the browser. | It is not directly run on the browser. |
5. |
It is just a scripting language. | It supports object-oriented programming concepts like classes, interfaces, inheritance, generics, etc. |
6. |
It doesn't support optional parameters. | It supports optional parameters. |
7. |
It is interpreted language that's why it highlighted the errors at runtime. | It compiles the code and highlighted errors during the development time. |
8. |
JavaScript doesn't support modules. | TypeScript gives support for modules. |
9. |
In this, number and string are the objects. | In this, number and string are the interfaces. |
10. |
JavaScript doesn't support generics. | TypeScript supports generics. |
function addNumbers(a, b) {
return a + b;
}
var sum = addNumbers(15, 25);
document.write('Sum of the numbers is: ' + sum);
| Example: function sumValues(a, b) {return a + b;
}
var sum = addNumbers(15, 25);
console.log('Sum of the numbers is: ' + sum); |
<script>
function addNumbers(a, b) {
return a + b;
}
var sum = addNumbers(15, 25);
document.write('Sum of the numbers is: ' + sum);
</script>
function addNumbers(a, b) {
return a + b;
}
var sum = addNumbers(15, 25);
console.log('Sum of the numbers is: ' + sum);
Advantages of TypeScript over JavaScript
- TypeScript always highlights errors at compilation time during the time of development, whereas JavaScripTypeScript Tutorials out errors at the runtime.
- TypeScript supports strongly typed or static typing, whereas this is not in JavaScript.
- TypeScript runs on any browser or JavaScript engine.
- Great tooling supports with IntelliSense which provides active hints as the code is added.
- It has a namespace concept by defining a module.
- TypeScript takes a long time to compile the code.
- TypeScript does not support abstract classes.
- If we run the TypeScript application in the browser, a compilation step is required to transform TypeScript into JavaScript.
Disadvantages of TypeScript over JavaScript
FAQs (Frequently Asked Questions):
- What is the primary difference between JavaScript and TypeScript?
In summary, JavaScript is a scripting language with dynamic typing, whereas TypeScript is a statically typed superset of JavaScript that features optional type checking.
- Which option is superior for large-scale applications?
TypeScript is preferred for extensive applications because of its static typing and interfaces, coupled with enhanced tooling support that aids in maintaining and scaling even the most significant applications.
- Is it necessary to understand JavaScript prior to learning TypeScript?
Indeed, since TypeScript is developed on the foundation of JavaScript, having a solid understanding of JavaScript will make it easier for you to grasp TypeScript concepts.
- Is TypeScript quicker than JavaScript?
No, TypeScript must be compiled into JavaScript prior to execution. Since browsers are capable of only executing JavaScript, the runtime efficiency of TypeScript and JavaScript is equivalent.
- Can I use JavaScript libraries in a TypeScript project?
Indeed, TypeScript is compatible with JavaScript libraries. However, it is often necessary to install TypeScript declaration files, commonly referred to as @types packages.
- Are all browsers compatible with TypeScript?
Web browsers do not natively support TypeScript; however, they can execute it in any environment that accommodates scripting languages derived from JavaScript. TypeScript is transpiled into JavaScript, enabling browser compatibility.