![]() |
VOOZH | about |
TypeScript is a statically typed superset of JavaScript that compiles to plain JavaScript. It adds features like static typing, interfaces, generics, and modern ECMAScript support, helping developers catch errors early and build scalable applications with better tooling such as autocompletion, refactoring, and debugging.
A "Hello, World!" program is the simplest way to get started with any programming language. Here’s how you can write one using TypeScript.
In Browser (via Compilation)
Create a file hello.ts:
Now compile it into JavaScript using:
tsc hello.tsThis will generate a hello.js file, which can then be included in your HTML.
In Node.js Console: Simply run the compiled JavaScript:
node hello.jsOutput:
Hello, World!Comments are notes in your code that the TypeScript interpreter ignores. They’re great for explaining what your code does or for testing purposes.
TypeScript’s flexibility extends to both the client-side and server-side, allowing developers to create complete, scalable applications. Here’s how it functions in each environment:
Client-Side:
Server-Side:
Here are the detailed difference between JavaScript and TypeScript:
| JavaScript | TypeScript |
|---|---|
| A lightweight, interpreted scripting language used for web development. | A superset of JavaScript that adds static typing and advanced features. |
| Dynamically typed (types are checked at runtime). | Statically typed (optional) – types are checked at compile time. |
| Runs directly in browsers or Node.js (no compilation needed). | Must be compiled to JavaScript using the TypeScript compiler (tsc). |
| Errors appear only at runtime. | Errors are caught at compile time, reducing runtime bugs. |
| Prototype-based object-oriented programming. | Class-based object-oriented programming with interfaces, access modifiers, generics. |
| Supports ES6+ features depending on the environment. | Supports all modern ES6+ features, plus extra TypeScript-only features. |
| Basic editor support, limited IntelliSense. | Rich tooling with autocompletion, type checking, and refactoring support. |
| Runs everywhere (browser, Node.js, servers). | Used in large-scale applications, compiled to JS for execution. |
Here are the steps for adding TypeScript file in html code:
Use the TypeScript compiler (tsc) to transpile types.ts into JavaScript. Open your terminal and run:
tsc types.tsThis command generates a types.js file containing the equivalent JavaScript code.
Open index.html in a web browser.
Despite its power, TypeScript has some limitations to consider: