Lesson 2 • Beginner

    Basic Types 🧱

    Master every built-in type TypeScript offers — from primitives and arrays to tuples, enums, and the critical difference between any and unknown.

    What You'll Learn in This Lesson

    • • Primitive types: string, number, boolean, null, undefined
    • • Arrays with typed elements and tuples with fixed structure
    • • Enums: numeric, string, and const enums
    • • Special types: any, unknown, void, never
    • • Type assertions and type narrowing techniques

    1️⃣ Primitive Types

    TypeScript's primitive types mirror JavaScript's but add compile-time checking. The most used are string, number, and boolean. Note: TypeScript uses lowercase (string) not uppercase (String).

    Try It: Primitives

    string, number, boolean, null, undefined, bigint, symbol

    Try it Yourself »
    JavaScript
    // TypeScript Primitive Types
    console.log("=== Primitive Types ===");
    console.log();
    
    // string
    let firstName = "Alice";
    let greeting = `Hello, ${firstName}!`;
    console.log("string:  let firstName = \"" + firstName + "\"");
    console.log("         template: \`Hello, \${firstName}!\` → " + greeting);
    console.log();
    
    // number (integers AND decimals — no separate int/float)
    let age = 28;
    let price = 9.99;
    let hex = 0xFF;
    let binary = 0b1010;
    console.log("number:  let age = " + age + "  (integer)");
    c
    ...

    2️⃣ Arrays, Tuples, and Enums

    Arrays enforce a single element type. Tuples add positional type constraints. Enums create named constants — string enums are the most useful in practice.

    Try It: Arrays & Tuples

    Typed arrays, fixed-structure tuples, and enum patterns

    Try it Yourself »
    JavaScript
    // Arrays, Tuples, and Enums
    console.log("=== Typed Arrays ===");
    let numbers = [1, 2, 3, 4, 5];
    let names = ["Alice", "Bob", "Charlie"];
    console.log("let numbers: number[] = " + JSON.stringify(numbers));
    console.log("let names: string[] = " + JSON.stringify(names));
    console.log("let mixed: (string | number)[] = " + JSON.stringify(["Alice", 28, "Bob", 32]));
    console.log();
    
    // Alternative syntax
    console.log("// Alternative generic syntax:");
    console.log("let scores: Array<number> = [95, 87, 92];
    ...

    3️⃣ Special Types & Type Narrowing

    any disables type checking (avoid it). unknown is the safe alternative that forces you to narrow the type before use. void means no return, and never means the function never completes normally.

    Try It: Special Types

    any vs unknown, type narrowing, void, never, and assertions

    Try it Yourself »
    JavaScript
    // Special Types: any, unknown, void, never
    console.log("=== any — The Escape Hatch ===");
    console.log("let data: any = 42;");
    console.log("data = 'hello';      // ✅ No error");
    console.log("data = [1, 2, 3];    // ✅ No error");
    console.log("data.foo.bar.baz;    // ✅ No error — but 💥 at runtime!");
    console.log("⚠️ 'any' disables ALL type checking. Avoid it!");
    console.log();
    
    console.log("=== unknown — The Safe Alternative ===");
    console.log("let input: unknown = getUserInput();");
    console.log(
    ...

    ⚠️ Common Mistakes

    ⚠️
    Using String instead of string — Always use lowercase types. String is the wrapper object, not the primitive.
    ⚠️
    Overusing type assertionsas bypasses type checking. Prefer type guards (typeof, instanceof) for safety.
    💡
    Pro Tip: Use const enums for zero runtime overhead — they compile to inline values.

    📋 Quick Reference — Types

    TypeExample
    stringlet s: string = "hi"
    numberlet n: number = 42
    booleanlet b: boolean = true
    Arraylet a: number[] = [1,2]
    Tuple[string, number]
    Enumenum Dir { Up, Down }
    unknownlet x: unknown = input()
    neverfunction fail(): never

    🎉 Lesson Complete!

    You know all the basic types! Next, learn to define complex object shapes with interfaces and type aliases.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy PolicyTerms of Service