Introduction to TypeScript
TypeScript is a programming language built on JavaScript. It adds static typing, helps identify errors during development, and makes applications easier to maintain.
What is TypeScript?
TypeScript is an open-source programming language developed by Microsoft. It extends JavaScript by adding a type system and other development features.
TypeScript code is generally compiled into JavaScript, which can run in browsers and JavaScript environments. It is commonly used to develop web applications, frontend projects, and large-scale software applications.
In JavaScript, a variable can receive values of different types. TypeScript allows developers to specify types such as string and number so that incompatible assignments can be reported during development.
JavaScript vs TypeScript
| Feature | JavaScript | TypeScript |
|---|---|---|
| Typing | Dynamic typing | Static type checking is available |
| Compilation | Usually runs directly in a JavaScript environment | Usually compiles into JavaScript |
| Error Detection | Many errors appear at runtime | Type-related errors can be detected during development |
| Type Annotations | Not supported as TypeScript annotations | Supports type annotations |
| Use Cases | Web pages and applications | Web applications and larger codebases |
Why Use TypeScript?
- Type Safety: Helps identify incompatible values during development.
- Better Readability: Type annotations make the expected data easier to understand.
- Developer Tools: Provides useful editor support, autocomplete, and navigation.
- Maintainability: Helps teams understand and update larger codebases.
- JavaScript Compatibility: Works with the JavaScript ecosystem.
Your First TypeScript Program
The following example declares a string variable and a number variable using explicit type annotations.
Example
let studentName: string = "Rahul";
let age: number = 22;
console.log(studentName);
console.log(age);
Rahul
22
Here, string specifies that
studentName should contain a string value.
Similarly, number specifies the type of
age.
How Type Checking Works
TypeScript checks whether values are compatible with their declared types. For example, assigning a string to a variable declared as a number produces a type-checking error.
Example
let marks: number = 85;
// Type error: a string cannot be assigned to a number.
marks = "Eighty Five";
TypeScript Execution Process
A typical TypeScript workflow includes writing a TypeScript file, checking and compiling it, and then executing the resulting JavaScript.
| Step | Action | Result |
|---|---|---|
| 1 | Write TypeScript | Create a file such as app.ts. |
| 2 | Check and compile | TypeScript reports type errors and can generate JavaScript. |
| 3 | Generate JavaScript | A JavaScript file such as app.js can be produced. |
| 4 | Run JavaScript | Execute it in a browser or JavaScript runtime. |
Compilation Command
tsc app.ts
This command uses the TypeScript compiler to compile
app.ts into JavaScript, provided the compiler
is installed and no blocking errors prevent output.
Where Is TypeScript Used?
- Frontend web application development.
- Large JavaScript projects and team-based development.
- Applications built with frameworks such as Angular.
- Node.js applications and backend development.
- Reusable libraries and developer tools.
Important Points
- TypeScript extends JavaScript with a type system.
- Type annotations include types such as string and number.
- TypeScript can detect many errors before code runs.
- TypeScript is commonly compiled into JavaScript.
- TypeScript types do not automatically provide runtime validation.
Summary
TypeScript extends JavaScript with static type checking, type annotations, and helpful development tools. It helps developers find type-related errors, organize code, and maintain larger applications. TypeScript code is generally compiled into JavaScript before execution.