Back to questions
JuniorTheoryCommon
What does tsc report, and what does the program print?
const data: any = JSON.parse('{"price": 100}');
console.log(data.price.toFixed(2));
console.log(data.cost.toFixed(2));tsc is silent; it prints 100.00, then a TypeError
- Expecting tsc to notice the missing
cost:dataisany, so tsc checks none of its properties, and the mistake shows up only at run time. - Expecting
undefinedon the last line:data.costisundefined, and callingtoFixedonundefinedthrows aTypeError.
- What would tsc report here if
datahad the typeunknown? - Why is a property read from an
anyvalue also of typeany?