Back to questions
JuniorTheoryCommon
What does the program print?
const raw: unknown = "42";
const n = raw as number;
console.log(n + 1);421
- Expecting
43:as numberconverts nothing, sonstays the string "42", and+ 1appends the digit. - Expecting a
TypeErrorat run time:asleaves no check in the JavaScript, so nothing is thrown.
- How would you turn the string into a number so that the program prints
43? - Why does tsc accept
raw as numberhere but reject"42" as number?