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
// 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
// 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
// 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
String instead of string — Always use lowercase types. String is the wrapper object, not the primitive.as bypasses type checking. Prefer type guards (typeof, instanceof) for safety.const enums for zero runtime overhead — they compile to inline values.📋 Quick Reference — Types
| Type | Example |
|---|---|
| string | let s: string = "hi" |
| number | let n: number = 42 |
| boolean | let b: boolean = true |
| Array | let a: number[] = [1,2] |
| Tuple | [string, number] |
| Enum | enum Dir { Up, Down } |
| unknown | let x: unknown = input() |
| never | function 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.