I have just came across this super simple example where typescript would not guard enough and would let readonly object been passed where mutable is expected.
Following compiles in TS 5.1.6 (and all other version I tested):
type ReadonlyObj = {
readonly x:number;
}
type MutableObj = {
x:number;
}
function mutate(mutableObj:MutableObj) {
mutableObj.x = 1;
}
const readonlyObj:ReadonlyObj = {x:0};
mutate(readonlyObj);
Should this be reported, or it is intended? If intended, how can I make sure my readonly object (and its parameters) is (compile time) protected from being mutated? Is there any compiler flag to define?