Back to questions
JuniorTheoryCommon
What does tsc report for this program?
type Size = "S" | "M" | "L";
function order(size: Size) {
console.log(size);
}
let mine = "M";
order(mine);An error: type string does not fit the parameter type Size
- Expecting silence because
mineholds"M":letwidens its type tostring, andstringdoes not fitSize. - Expecting a type check at run time: types are erased, so the JavaScript prints
Mwithout any error.
- Name two ways to change the program so that
order(mine)compiles. - Why does
order("M")compile whileorder(mine)does not?