Back to questionsWhat does
JuniorTheoryCommon
What does tsc report for this code?
let total: number = 0;
total = "10";An error: a string cannot be assigned to a variable of type number
- Expecting the string
"10"to become the number10: the quotes make it a string, and a type annotation never converts a value. - Expecting the error only at run time, as in JavaScript:
tscreports it before the program runs, from the code's text.
- How would you change the code so that
totalgets the number written in the string"10"? - What does
tscreport if the first line isconst total: number = 0;?