Back to questions
JuniorTheoryCommon
On which line does tsc report an error?
type Order = { readonly id: number; total: number };
const o: Order = { id: 1, total: 50 };
o.total = 60;
o.id = 2;
console.log(o.id, o.total);4
- Expecting
readonlyto forbid settingidin the literal too: the value is given when the object is created, and only later writes are errors. - Expecting
const oto freeze every property:constonly keeps another object out ofo, soo.total = 60is allowed.
- Does
readonlystop the write on line 4 when the compiled JavaScript runs? - How would you make
totalread-only as well, and what would tsc then report about line 3?