What is TypeScript?
TypeScript is a programming language developed by Microsoft that builds on JavaScript by adding static typing.
It allows developers to define data types within their code, helping to catch errors during development rather than at runtime. This improves code reliability and enhances developer productivity, as errors can be identified early, making the development process smoother and more predictable.
The Birth of TypeScript
JavaScript was originally created for client-side scripting in the mid-90s. However, as websites became more complex over time, developers faced issues like bugs, messy code, and lack of proper error and type checking.
To address these challenges, Microsoft created TypeScript in 2012, led by Anders Hejlsberg, to provide a more robust solution for large-scale JavaScript development.
Key Features of TypeScript
1) Static Type Checking
TypeScript allows you to check and assign types to variables, parameters, and functions.
This helps catch errors early during development and makes your code more readable and maintainable.
let name: string = "Abhishek"; // Correct
name = 123; // Error: Type 'number' is not assignable to type 'string'
2) Type Inference
- TypeScript can Automatically determine the type of a variable based on it's assigned value
let age = 19; // TypeScript infers 'age' as a number
age = "one"; // Error: Type string is not assignable to type number
- TypeScript allows you to write less code while still maintaining type safety, making your code more concise and reliable.
3) Object-Oriented Programming (OOP) Support
- TypeScript provides full support for Object-Oriented Programming (OOP) concepts such as classes, interfaces, inheritance, and more.
4) JavaScript is TypeScript
- Any valid JavaScript code with a
.js
extension can be converted to TypeScript simply by changing the file extension from.js
to.ts
. - This makes transitioning to TypeScript easy for developers already familiar with JavaScript.
5) TypeScript Supports JavaScript Libraries
- All existing JavaScript libraries are valid in TypeScript as well.
- As a result, developers can seamlessly use and integrate JavaScript libraries, tools, and frameworks within TypeScript projects.
6) Compilation
- The TypeScript compiler provides built-in error checking. If there is a syntax error in the code, TypeScript will generate a compilation error, allowing you to catch and fix the issue before runtime.
Structure of TypeScript
- TypeScript code cannot be directly interpreted by the browser, so it must be compiled into plain JavaScript.
- To accomplish this, we use the TypeScript Compiler (tsc) to convert TypeScript code into JavaScript code that the browser can understand and execute.
TypeScript Compiler
- The TypeScript Compiler (tsc) converts TypeScript (
.ts
) files into JavaScript (.js
), making them executable in the browser or Node.js environment.
Steps to Use TypeScript
1) Install TypeScript
Open a terminal and run the following command to install TypeScript globally.
If you don't have Node.js installed on your system, first install Node.js, which includes npm (Node Package Manager).
npm install --global typescript
2) Verify Installation
- To verify that TypeScript has been installed successfully, run the following command in your terminal:
tsc --version
3) Compile a TypeScript File
After verifying the installation, you can compile your
.ts
file into JavaScript using the TypeScript compiler.To compile a TypeScript file (
example.ts
), run the following command in your terminal:
tsc example.ts
- This will generate an example.js file in the same directory, which can be executed in the browser or Node.js.
Top comments (0)