TypeScript helps you write safer and clearer JavaScript code by adding types. It catches mistakes before you run your program.
0
0
What is TypeScript
Introduction
When you want to avoid bugs by checking your code early.
When working on big projects with many files and people.
When you want better tools like auto-completion and error hints in your editor.
When you want to use new JavaScript features but keep compatibility with older browsers.
When you want to write code that is easier to understand and maintain.
Syntax
Typescript
let message: string = "Hello, TypeScript!"; function add(a: number, b: number): number { return a + b; }
Type annotations like : string or : number tell TypeScript what type a variable or function should have.
TypeScript code needs to be converted (compiled) to JavaScript before running in browsers or Node.js.
Examples
This declares a variable
age that must hold a number.Typescript
let age: number = 25;
This function takes a string and returns a string.
Typescript
function greet(name: string): string { return `Hello, ${name}!`; }
This variable holds a true or false value.
Typescript
let isActive: boolean = true;
Sample Program
This program defines a function to multiply two numbers and prints the result.
Typescript
function multiply(x: number, y: number): number { return x * y; } const result = multiply(4, 5); console.log(`4 times 5 is ${result}`);
OutputSuccess
Important Notes
TypeScript is a superset of JavaScript, so all JavaScript code is valid TypeScript.
Using types helps you find errors early, before running your code.
You need a tool called the TypeScript compiler (tsc) to turn TypeScript into JavaScript.
Summary
TypeScript adds types to JavaScript to help catch errors early.
It improves code clarity and tooling support.
You write TypeScript, then compile it to JavaScript to run it.