【发布时间】:2022-06-27 22:55:45
【问题描述】:
有没有办法使代码编译失败,如下例所示:
interface Ro {
readonly x: string;
}
const modify = (rw: {x: string;}) => rw.x = 'bye';
const use = (v: Ro) => {
console.log(v.x);
//v.x = 'bye'; Compiler error: "Cannot assign to 'x' because it is a read-only property."
modify(v);// No errors, not even warnings.
}
const ro: Ro = {x: "hi"}
use(ro);
console.log(ro.x);// readonly field has been changed!
【问题讨论】:
标签: typescript