2.5 Hours

1. The TypeScript Paradigm & Core Types

Architecture

The Static Typing Paradigm

JavaScript is a Dynamically Typed language. This means types are determined at runtime (when the browser is actually executing the code). You can declare let age = 25; and on the very next line write age = "Twenty Five";. The JavaScript engine will allow this, which often leads to catastrophic bugs when you try to perform math on what you thought was a number, but is actually a string.

TypeScript, developed by Microsoft, introduces Static Typing. It is a strict superset of JavaScript. This means every valid JavaScript file is technically a valid TypeScript file, but TypeScript adds a rigorous layer of rules on top. In TypeScript, types are checked at Compile Time (when you save the file in your editor). If you try to assign a string to a number variable, TypeScript will throw a fatal error and refuse to build your application.

The Compiler (tsc) and Type Erasure

The TypeScript Paradigm: Static Checking & Type Erasure TypeScript checks types at compile-time, then completely erases them, leaving pure JavaScript at runtime 1. COMPILE-TIME (.ts) interface User { id: number; name: string; } ✓ Catches bugs BEFORE deploy tsc build TYPE ERASURE Types stripped away 2. RUNTIME (.js) const user = { id: 1, name: 'Ama' }; Zero runtime weight Pure JavaScript

It is absolutely critical to understand that browsers cannot read TypeScript. Chrome, Safari, and Node.js have no idea what an interface or a type is.

When you finish writing your TypeScript code, a compiler (like tsc, Babel, or Vite) translates your .ts files into standard .js files. During this process, a concept called Type Erasure occurs. The compiler strips away every single TypeScript rule, interface, and type annotation, leaving behind pure, highly optimized JavaScript.

TypeScript is purely a development tool. It exists solely to help developers catch errors before the code ever reaches the user.

TypeScript
// BEFORE COMPILATION (TypeScript)
interface User {
  id: number;
  name: string;
}

function greet(user: User): string {
  return `Hello, ${user.name}`;
}

// AFTER COMPILATION (JavaScript - Type Erasure)
// The interface and type annotations are completely stripped away!
function greet(user) {
  return `Hello, ${user.name}`;
}

Core Primitives & Type Inference

TypeScript supports all JavaScript primitives: string, number, boolean, bigint, and symbol. You can explicitly define the type of a variable using a colon : followed by the type name.

However, TypeScript also possesses a powerful Type Inference engine. If you initialize a variable with a value, TypeScript locks in that type automatically without you writing it. Furthermore, the way you declare a variable (let vs const) drastically changes how inference works (Widening vs Literal Types).

TypeScript
// 1. Explicit Typing (Developer manually defines the type)
let username: string = 'Alice';
let age: number = 30;
let isOnline: boolean = true;

// 2. Type Inference with 'let' (Type Widening)
// TS infers 'score' is of type 'number' (any number).
let score = 100;
score = 200; // Valid

// 3. Type Inference with 'const' (Literal Types)
// TS infers 'pi' is strictly the literal value 3.14. It is not just 'number'.
const pi = 3.14; 
// pi = 3.15; // ERROR: Cannot assign to 'pi' because it is a constant.

// ERROR: Type 'string' is not assignable to type 'number'.
// score = "High Score";

Arrays and Multi-Dimensional Data

When typing arrays, you must define what kind of data is allowed inside the array. You cannot mix strings and numbers in a standard typed array.

TypeScript
// Standard Array Syntax: Type[]
let prices: number[] = [9.99, 14.5, 20.0];

// Generic Array Syntax: Array<Type> (Identical behavior)
let tags: Array<string> = ['React', 'TypeScript'];

// Multi-Dimensional Arrays (Arrays inside Arrays)
let grid: number[][] = [
  [1, 2, 3],
  [4, 5, 6]
];

// ERROR: Argument of type 'string' is not assignable to parameter of type 'number'.
// prices.push("Free");

Tuples: Fixed-Length Architectures

Sometimes, you need an array with a fixed length where each specific index has a specific type. This is called a Tuple. React engineers use Tuples constantly—the useState hook returns a Tuple!

Tuples can also have optional parameters (using ?) or rest elements for dynamic trailing data.

TypeScript
// Standard Tuple: Strictly length 2. Index 0 is string, Index 1 is number.
let userRecord: [string, number] = ['Alice', 30];

// ERROR: Type 'number' is not assignable to type 'string'.
// userRecord = [30, "Alice"]; // Order is strictly enforced!

// Optional Elements in Tuples (must be at the end)
let point3D: [number, number, number?] = [10, 20]; // Z-axis is optional

// Rest Elements in Tuples (Capture remaining arguments)
let logEntry: [string, ...number[]] = ["Error", 404, 500, 503];

The Danger of any and unknown

If you don't know what type a variable will be, you can use the any type. However, using any is a massive anti-pattern. It completely disables the TypeScript compiler for that variable, reverting it back to standard, chaotic JavaScript. Most enterprise codebases enforce the noImplicitAny compiler flag to ban its use.

TypeScript
// ANTI-PATTERN: Using 'any'
let mysteriousData: any = 'Hello';
mysteriousData = 42;
mysteriousData.fakeMethod(); // No error thrown! The app will crash at runtime.

If you truly do not know what data is coming in (e.g., fetching a random API payload or reading user input), the strict architectural standard is to use the unknown type. It forces you to write code to check what the type is (Type Guarding) before you are allowed to use it.

TypeScript
// CORRECT ARCHITECTURE: Using 'unknown'
let data: unknown = 'Hello';

// data.toUpperCase(); // ERROR: Object is of type 'unknown'.

// You must narrow the type first (Type Guarding)
if (typeof data === 'string') {
  console.log(data.toUpperCase()); // Safe! TS knows it is a string here.
}

Knowledge Check

Ready to test your understanding of 1. The TypeScript Paradigm & Core Types?

Back