【发布时间】:2019-07-23 14:26:45
【问题描述】:
为什么 typescript 不强制执行 readonly 关键字并阻止我们将只读属性传递给非只读属性,这不符合重点
let foo: {
readonly bar: number;
} = {
bar: 123
};
function iMutateFoo(foo: { bar: number }) {
foo.bar = 456;
}
iMutateFoo(foo); // The foo argument is aliased by the foo parameter
console.log(foo.bar); // 456!```
【问题讨论】:
-
我写了一个 ESLint 规则来防止这种情况:github.com/danielnixon/…
标签: typescript readonly