You do not have to label every variable. Inference means the checker works out a type from the code. In let minutes = 25, minutes is inferred as number. Assigning 30 later is allowed; assigning “thirty” is not. Inference keeps straightforward code readable.
Use the primitive names string, number, and boolean, with lowercase initials. Number includes integers and decimals; it also includes NaN and Infinity. A type is not a range check. String means text, not “text that contains a valid date.”
A const binding cannot be reassigned, so a simple constant can keep a more specific literal type. const mode = "study" can retain the exact value “study”; a let binding normally widens to string so it can change. You do not need to memorize every inference rule now. Ask what operations the value supports and whether it is meant to change.
Annotations are useful when the intended type is not obvious or when establishing a function or data boundary. They are usually redundant in const total: number = 10 + 5. Both styles work; prefer the least annotation that communicates the contract.
The next example combines values with a template literal, the same backtick syntax used in JavaScript. Interpolation formats values as text. It does not change the original numeric value.
Predict, check, then run
Before running, write down the output and explain which information the checker knows. Click Check types first, then Check & run.
let minutes = 20;
minutes = 30;
const completed = false;
const title = "Loops";
console.log(`${title}: ${minutes} min, done=${completed}`);Enable JavaScript to edit and type-check this example.
Check your prediction
Loops: 30 min, done=false Read a real compiler error
This declaration has no explicit annotation. Predict whether the checker still notices the incorrect reassignment.
let minutes = 25;
minutes = "30";Enable JavaScript to edit and type-check this example.
Check your diagnosis
It does: the initial value inferred number. Assign 30, or convert and validate text when your program genuinely receives text. Inference is checking, not an escape from it.
Annotations and inference both describe values
A variable can have a useful inferred type without an annotation. Add annotations when they state a boundary or clarify intent, not simply to repeat every obvious value. Inference follows the initializer and the surrounding context; it is not a record of every value the variable might eventually be assigned.
A number annotation does not promise a positive count, and a string annotation does not promise nonblank text. Keep runtime domain rules separate from these broad language categories. Later lessons introduce unions and validation for more precise contracts.
let minutes = 20;
minutes = 30;
const topic = "Types";
console.log(minutes + 5);
console.log(topic.toUpperCase());Enable JavaScript to edit and type-check this example.
Check your prediction
35
TYPES The checker inferred number and a string value. No explicit annotation was required for these operations, and the runtime executes ordinary JavaScript after checking.
const label: string = "";
const count: number = -3;
console.log(label.length);
console.log(count < 0);Enable JavaScript to edit and type-check this example.
Check your prediction
0
true These values satisfy their annotations. Empty text and negative counts may violate an application rule, but that rule is not represented by string or number alone.
Try the rule on new inputs
Compare a variable inferred from a number with one explicitly annotated number. Make a wrong-type assignment to each and read the diagnostics. Restore the code, then name one domain rule neither declaration enforces.
Your turn
Types & Inference: practise
Infer the types of title, minutes and completed from their initial values. Update minutes to 45 and completed to true, then print Types: 45 min, done=true.
const title = "Types";
let minutes = 15;
let completed = false;
// Update the changing values and print a message.Enable JavaScript to edit and type-check this example.
Hint 1: the idea
Let allows reassignment, but not a change to an unrelated type.
Hint 2: a first step
Assign a number to minutes and a boolean to completed before formatting the message.
Show one solution
const title = "Types";
let minutes = 15;
let completed = false;
minutes = 45;
completed = true;
console.log(`${title}: ${minutes} min, done=${completed}`);Enable JavaScript to edit and type-check this example.
Types: 45 min, done=trueExplain the type of each value and which mistake the checker would catch. Compare your approach before copying this answer.
Change it and explain it
Try assigning “true” instead of true. Explain the difference without running. Then make completed a const and observe the reassignment error. Restore a working version and explain when you would choose const over let.
Completion is saved in this browser only. It does not measure mastery.