These topics cover the core syntax and features TypeScript adds to JavaScript:
- What is TypeScript and Why Use It
- Installing & Setting Up TypeScript
- TypeScript Compiler (
tsc) & Configuration (tsconfig.json) - Basic Types
- Type Annotations vs Type Inference
- Arrays and Tuples
- Enums
- Type Aliases
- Union and Intersection Types
- Literal Types
constvsletwith Types
TypeScript is a superset of JavaScript that adds static typing and other features to improve code quality and productivity.
- β¨ Error Prevention: Catches bugs during development.
- π Improved Readability: Explicit types make code easier to understand.
- π Modern Features: Supports ES6+ features and compiles to older JavaScript.
function add(a: number, b: number): number {
return a + b;
}
add(5, "10"); // β Error: Argument of type 'string' is not assignable to parameter of type 'number'.- π οΈ Install TypeScript globally:
npm install -g typescript
- π Initialize a project:
npm init -y
- π¦ Install TypeScript locally:
npm install --save-dev typescript
- π Create a
tsconfig.jsonfile:npx tsc --init
- π Compile
.tsfiles to.js:npx tsc index.ts
The TypeScript compiler (tsc) converts TypeScript into JavaScript. The tsconfig.json file configures the compiler.
{
"compilerOptions": {
"target": "ES6", // π οΈ Specify JavaScript version
"strict": true, // π Enable strict type checking
"outDir": "./dist" // π Output directory
},
"include": ["src/**/*"], // π Files to include
"exclude": ["node_modules"] // π« Files to exclude
}- π’
number, πstring, βboolean:let age: number = 25; let name: string = "Alice"; let isStudent: boolean = true;
- π
any: Allows any type (use sparingly).let value: any = 42; value = "Hello";
- π
unknown: Safer alternative toany.let value: unknown = "Hello"; if (typeof value === "string") console.log(value.toUpperCase());
- π«
void: For functions that donβt return a value.function logMessage(message: string): void { console.log(message); }
- β
never: For functions that never return.function throwError(message: string): never { throw new Error(message); }
- ποΈ Type Annotations: Explicitly specify types.
let age: number = 25;
- π Type Inference: TypeScript infers the type.
let age = 25; // Inferred as number
- π Arrays: Store multiple values of the same type.
let numbers: number[] = [1, 2, 3];
- π Tuples: Fixed-length arrays with specific types.
let person: [string, number] = ["Alice", 25];
Enums define a set of named constants.
enum Direction {
Up,
Down,
Left,
Right,
}
let move: Direction = Direction.Up;
console.log(move); // 0Type aliases create custom types.
type Point = {
x: number;
y: number;
};
let point: Point = { x: 10, y: 20 };- π Union Types: A value can be one of several types.
let value: string | number = "Hello"; value = 42;
- π Intersection Types: Combine multiple types.
type A = { a: number }; type B = { b: string }; type C = A & B; let obj: C = { a: 1, b: "Hello" };
Literal types allow specific values as types.
let direction: "up" | "down" = "up";- π Use
constfor variables that wonβt change:const name: string = "Alice";
- π Use
letfor variables that may change:let age: number = 25; age = 26;