SyntaxStudy
Sign Up
TypeScript What Is TypeScript and Why Use It
TypeScript Beginner 1 min read

What Is TypeScript and Why Use It

TypeScript is a strongly typed superset of JavaScript developed and maintained by Microsoft. It compiles down to plain JavaScript, which means any valid JavaScript is also valid TypeScript. TypeScript adds optional static types, interfaces, enums, and other features that help developers catch errors at compile time rather than at runtime. The primary motivation for using TypeScript is developer productivity and code reliability. In large codebases, dynamic typing in JavaScript makes it easy to introduce subtle bugs that only surface at runtime. TypeScript's type checker acts as a safety net, flagging type mismatches, missing properties, and incorrect function arguments before the code ever runs. TypeScript has widespread adoption in modern front-end and back-end development. Frameworks like Angular are built entirely in TypeScript, and React, Vue, and Node.js all have excellent TypeScript support. Learning TypeScript is a high-value investment because it makes code more self-documenting, refactoring safer, and team collaboration significantly smoother.
Example
// TypeScript adds types on top of JavaScript

// Plain JavaScript — no type safety
function addJS(a, b) {
    return a + b;
}
console.log(addJS(2, "3")); // "23" — bug, no error thrown

// TypeScript — types prevent the bug
function addTS(a: number, b: number): number {
    return a + b;
}
// addTS(2, "3"); // Error: Argument of type 'string' is not assignable to parameter of type 'number'
console.log(addTS(2, 3)); // 5

// Basic type annotations
let username: string = "Alice";
let age: number = 30;
let isActive: boolean = true;
let scores: number[] = [95, 87, 72];

// TypeScript infers types when you assign a value
let city = "London"; // inferred as string
// city = 42;        // Error: Type 'number' is not assignable to type 'string'

// Object with inline type annotation
let user: { name: string; age: number } = {
    name: "Bob",
    age: 25,
};

console.log(`${user.name} is ${user.age} years old.`);