๐ค Basic Types & Type System
๐ค Basic Types & Type System
The Primitive Types
TypeScript includes seven primitive types from JavaScript, plus two special types:
string
Text values. Can use single quotes, double quotes, or backticks.
number
All numeric values: integers, floats, Infinity, NaN.
boolean
Only two values: true or false.
null & undefined
Special values representing absence. Usually used with unions.
bigint
Numbers larger than Number.MAX_SAFE_INTEGER (rare).
symbol
Unique identifiers (advanced; rarely used in beginner code).
๐ Type Annotations
Annotations on Function Parameters & Returns
You can annotate parameters and specify what a function returns:
๐ง Type Inference
However, inference has limits. Sometimes you need explicit annotations:
Best Practice: When to Annotate
| Situation | Recommendation |
|---|---|
Clear from assignment (e.g., const x = 5) |
Skip annotation, let inference work |
| Function parameters | Always annotate |
| Function return values | Highly recommended (documents intent) |
| Complex/unclear types | Always annotate for clarity |
Variable initialized to null or undefined |
Always annotate (inference too vague) |
๐ญ Special Types: any & unknown
The any Type
any is an "escape hatch" that disables type checking. Use it sparingly!
Why avoid `any`? It defeats the entire purpose of TypeScript. You lose type safety and IDE support.
The unknown Type
unknown is the safer, stricter cousin of any. It's type-safe because it forces you to check what the value actually is before using it.
any vs unknown
unknown instead of any. Always. unknown requires you to check the type before using the value, which keeps your code safe.
๐ Type Narrowing (Preview)
As we saw with `unknown`, TypeScript can narrow types based on your code. This happens with:
- typeof checks:
typeof x === "string" - Instanceof checks:
x instanceof Date - Truthiness checks:
if (x) { ... } - Equality checks:
if (x === null) { ... }
We'll dive deep into type narrowing in a later chapter, but understanding it now will help you write safer TypeScript.
๐ป Coding Challenges
Challenge 1: Type Annotation Errors
Fix the type errors in this code by adding correct type annotations:
Goal: Add type annotations so each variable/parameter has the correct type.
Challenge 2: Inference vs Annotation
Which of these variables need explicit type annotations, and which can rely on inference?
Goal: Identify which ones need annotations and explain why.
Challenge 3: any vs unknown
Write a function that safely handles unknown input. Check if it's a number, and if so, double it. Otherwise, return 0.
Goal: Demonstrate type narrowing with `unknown`.
While you can let TypeScript infer types for variables, always be explicit with function parameters and return types. This makes your code self-documenting and easier for others (and future you!) to understand. A 10-second type annotation saves 10 minutes of confusion later.
๐ฏ What's Next
Now that you understand primitives and type annotations, we'll explore how to work with collections: objects, arrays, and tuples. You'll learn how to describe the shape of complex data structures.