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=falseRead 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
TYPESThe 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
trueThese 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.
Debug a plausible mistake
Repair a value without weakening its type
The total represents numeric minutes and must print 30. Keep minutes typed as number. If the source value arrives as text, perform an explicit conversion rather than asserting that the string is a number.
Use Check types to inspect the genuine diagnostic before repairing the code. Keep the intended contract; do not bypass the checker with any, a broad assertion or an ignore comment.
const source = "25";
const minutes: number = source;
console.log(minutes + 5);Enable JavaScript to edit and type-check this example.
Hint 1: the idea
An annotation checks a relationship; it does not convert the runtime value.
Hint 2: a first step
Supply a numeric value or convert the source text with Number before assigning.
Show one solution
const source = "25";
const minutes: number = Number(source);
console.log(minutes + 5);30Number(source) is the runtime step that turns "25" into 25; the : number annotation only checks that the value assigned really is a number. Without the conversion, TS2322 reported string assigned to number. Had the checker not stopped it, minutes + 5 would have printed the text "255" instead of 30.
Your turn
Types & Inference: practice
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=truelet minutes = 15 is inferred as number and let completed = false as boolean, so 45 and true are accepted with no annotations. minutes = "45" would be rejected with TS2322. title is a const because it never changes; a reassignment to it would also be an error.
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.