Last Updated : 23 Jul, 2025
TypeScript is a syntactic superset of JavaScript that adds optional static typing, making it easier to write and maintain large-scale applications.
TypeScript is essentially JavaScript with additional features, most notably the ability to use type annotations. While JavaScript is dynamically typed, meaning types are determined at runtime, TypeScript allows developers to define types at compile time. This can help catch errors early in the development process and makes code easier to understand and maintain.
Key Features of TypeScript 1. Static Type Checking (Optional)TypeScript allows you to check and assign types to variables, parameters, and function return values. While this step requires a little more effort, it significantly improves code quality. Optional static typing helps prevent bugs and makes your code more readable.
2. Class-Based ObjectsOne of TypeScript’s standout features is its support for classes. Unlike JavaScript’s prototype-based approach, TypeScript lets you write true object-oriented code. You can create classes, define constructors, and use inheritance and access modifiers (public, private, protected).
3. ModularityTypeScript promotes modularity. By using modules, you can organize your code into smaller, reusable pieces. This modularity enhances maintainability and collaboration among team members.
4. ES6 FeaturesTypeScript embraces ECMAScript 6 (ES6) features. If you’re familiar with ES6 syntax (arrow functions, template literals, destructuring, etc.), you’ll feel right at home with TypeScript.
5. Syntactical SugaringTypeScript’s syntax is closer to that of high-level languages like Java. It’s like a sweetener for developers—more concise and expressive.
Structure of TypeScriptTypeScript Code cannot be interpreted by the Browser directly so there is a need to compile the TypeScript code into plain JavaScript Code, for this purpose we need the TypeScript Compiler (tsc).
TypeScript Compiler (tsc)A common way to use TypeScript is by utilizing the official TypeScript compiler, which transpiles TypeScript code into JavaScript. To get started, you'll need to set up the compiler for your local project. Many popular code editors, such as Visual Studio Code, have built-in TypeScript support, providing real-time error detection and suggestions as you write your code. This integration makes it easier to work with TypeScript, ensuring you catch potential issues early in the development process.
Steps for Adding TypeScript File in HTML Code 1. Create the TypeScript File (types.ts)
let myString;
myString = 'Hello from ts';
console.log(myString);
Use the TypeScript compiler (tsc) to transpile types.ts into JavaScript. Open your terminal and run:
tsc types.ts
This command generates a types.js file containing the equivalent JavaScript code.
3. Create the HTML File (index.html) HTML
<html lang="en">
<head>
</head>
<body>
<h2>Welcome to GeeksforGeeks</h2>
<p>Default code has been loaded into the Editor.</p>
<script src="types.js"></script>
</body>
</html>
Open index.html in a web browser.
5. OutputIntroduction to TypeScript: Why Use It?
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4