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 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'.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"];
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'.To annotate a function parameter explicitly, you write the parameter name, a colon, then its ____.
How do you write the type annotation for an array that should only ever contain numbers?
Function parameters are automatically inferred by TypeScript the same way variables with initial values are.
Basic Types
10 questions · pass with 60% — just like in the app.
Which type annotation would you use for a variable that holds true or false?
TypeScript has separate types for whole numbers and decimal numbers.
let city: string = "Accra"; is an example of an explicit type ____.
What type does TypeScript infer for let total = 250;?
TypeScript can only infer a variable's type automatically when it is given an ____ value.
A variable declared without an initial value and without an annotation is inferred as any.
Why do function parameters need explicit type annotations, unlike variables with an initial value?
An array that should only ever hold numbers is annotated as number____.
string[] and number[] both use the same square-bracket syntax, just with a different element type.
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 →