Swipe
SwipeSwipe
Swipe Apps

Paths

Motivation

Information

Basic Types · Lesson 6 of 12

Explicit Type Annotations

A free lesson from Swipe Coding · TypeScript, level 2 · updated October 2026

By the Swipe team, founded by Colin · how we make lessons

🤖
Inference is great until a function needs to know what's coming in — that's where you take over. ✍️

Inference works great for variables with an initial value, but it can't read your mind about function parameters — TypeScript has no way to know what a function is supposed to receive unless you tell it. That's where explicit annotations stop being optional and become essential.

function multiply(a: number, b: number): number {
  return a * b;
}

multiply(3, "4"); // Error: Argument of type 'string' is not assignable to parameter of type 'number'.
Each parameter gets its own : number annotation, and the : number written after the parentheses annotates the function's return value too.
🤖
Skip the annotation and TypeScript just shrugs — any goes in, any comes out. 🤷

Without those annotations, a and b would silently fall back to the any type, meaning TypeScript would allow absolutely anything to be passed in — multiply(3, "4") wouldn't be flagged at all, and the bug would only show up once the code actually ran.

let names: string[] = ["Kofi", "Lena", "Ines"];
The same [] pattern works for any type — string[] is an array of strings, boolean[] an array of booleans, and so on.

Arrays get their own annotation syntax: write the element type followed by square brackets. number[] means an array containing only numbers.

let scores: number[] = [92, 85, 78];
scores.push(100);   // fine — 100 is a number
scores.push("A+");  // Error: Argument of type 'string' is not assignable to parameter of type 'number'.
scores is locked in as an array of numbers — TypeScript checks every element added to it afterward, not just the ones it started with.
Fill in the blank

To annotate a function parameter explicitly, you write the parameter name, a colon, then its ____.

Quick check

How do you write the type annotation for an array that should only ever contain numbers?

True or false

Function parameters are automatically inferred by TypeScript the same way variables with initial values are.

Chapter quiz

Basic Types

10 questions · pass with 60% — just like in the app.

Quick check

Which type annotation would you use for a variable that holds true or false?

True or false

TypeScript has separate types for whole numbers and decimal numbers.

Fill in the blank

let city: string = "Accra"; is an example of an explicit type ____.

Quick check

What type does TypeScript infer for let total = 250;?

Fill in the blank

TypeScript can only infer a variable's type automatically when it is given an ____ value.

True or false

A variable declared without an initial value and without an annotation is inferred as any.

Quick check

Why do function parameters need explicit type annotations, unlike variables with an initial value?

Fill in the blank

An array that should only ever hold numbers is annotated as number____.

True or false

string[] and number[] both use the same square-bracket syntax, just with a different element type.

Quick check

What happens if you try to push the string "10" onto a variable annotated as number[]?

Keep your streak going in Swipe Coding

Swipe through every level of TypeScript on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.

Get the Swipe apps →