TypeScript · Junior · Question 5 of 75

What type does TypeScript infer for let x = 5;?

Explanation

Type inference: for let, the literal widens to number because the variable can be reassigned. For const x = 5, the type is the literal 5. Explicit annotations are needed where inference is impossible or imprecise - e.g. in function parameters.

Further reading

Problem with a question

TypeScript · Junior · question 5

Test yourself

Take the TypeScript · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz