Welcome to TypeScript: JavaScript with Types

TypeScript, created by Microsoft and first released in 2012, is a superset of JavaScript — every valid JavaScript file is already valid TypeScript. What TypeScript adds on top is a type system: you can describe what shape a variable, function parameter, or return value should have, and a separate compiler checks that your whole codebase […]

Basic Types and Type Inference

let age: number = 25;nlet name: string = “Tutoline”;nlet isActive: boolean = true;nlet tags: string[] = [“python”, “typescript”];nlet coordinates: [number, number] = [28.6, 77.2]; // a tuple: fixed length and types Type inference let age = 25; // TypeScript infers this is a number, no annotation needednage = “twenty-five”; // Error: Type ‘string’ is not […]

Interfaces and Type Aliases

Interfaces interface Course {n title: string;n lessonCount: number;n isFree?: boolean; // the ? makes this property optionaln}nnfunction describeCourse(course: Course): string {n return `${course.title} has ${course.lessonCount} lessons`;n}nndescribeCourse({ title: “TypeScript”, lessonCount: 7 }); // isFree can be omitted An interface describes the shape an object must have — which properties, and what type each one is. Any […]

Functions and Optional Parameters

function greet(name: string, greeting: string = “Hello”): string {n return `${greeting}, ${name}!`;n}nnfunction greetOptional(name: string, greeting?: string): string {n return `${greeting ?? “Hello”}, ${name}!`;n} A parameter with a default value (= “Hello”) is automatically optional. A parameter marked with ? is optional without a default, meaning it could be undefined inside the function — ?? (the […]

Generics

Generics let a function, interface, or class work with any type while TypeScript still tracks and checks exactly which type is being used each time — the same core idea covered in the Java and C++ courses on this site, adapted to TypeScript’s syntax. function firstItem<T>(items: T[]): T {n return items[0];n}nnconst firstCourse = firstItem([“Python”, “TypeScript”]); […]

Classes and Access Modifiers

class Course {n private title: string;n public lessonCount: number;nn constructor(title: string, lessonCount: number) {n this.title = title;n this.lessonCount = lessonCount;n }nn getTitle(): string {n return this.title;n }n}nnconst tsCourse = new Course(“TypeScript”, 7);nconsole.log(tsCourse.getTitle()); Access modifiers private restricts a property to being accessed only from inside the class itself — tsCourse.title from outside would be a compile […]

Working with tsconfig and What’s Next

tsconfig.json tsc –init This generates a tsconfig.json file, which controls how the TypeScript compiler behaves for your whole project — which JavaScript version to compile down to, which folders to include, and how strict the type checking should be. Every real TypeScript project has one. The strict flag {n “compilerOptions”: {n “strict”: true,n “target”: “ES2020”,n […]