Beginner · 10 min
Inference and reassignment
Let TypeScript infer a type and keep later assignments compatible.
Infer from an initializer
TypeScript can infer a type from an initial value. You do not need to annotate every variable. A variable initialized with a number normally continues to hold numbers.
let score = 3;
score = score + 2;
console.log(score);Output
5
Convert explicitly
Use Number() when text must become a number. Type annotations do not perform this conversion. Check real input before using it in calculations.
const entered = "6";
const seats = Number(entered);
console.log(seats + 1);Output
7
Put it into practice
- Read the types and predict the output before running the examples.
- Complete the task, then check your understanding with the practice questions.
Try it yourself
Code runs on this device. When you are signed in, drafts sync to your account.
Convert an entered count
Convert "entered" to a number before assigning it to "count". Display the updated count.
Use Number() on "entered" before assignment.