Readonly properties can be modified in derived classes
还没有人认领这个 Issue。
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 25/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- typescript
- 领域
- compilers
调研方向
未指定源文件或测试。先从链接的 Playground 复现和 readonly 修饰符及接口文档开始,然后追踪编译器如何处理类继承中的 readonly 成员;当预期规则确定,并且该行为由文档一致的回归测试覆盖时,即视为完成。
由索引模型根据 Issue 内容生成。
描述
This issue is created even if there was a pretty complete issue about the purpose of the readonly keyword ( https://github.com/Microsoft/TypeScript/issues/8496#issuecomment-217500742 ) and I'll just quote @RyanCavanaugh :
Basically we see properties as "possibly readonly" or "definitely readonly", and allow assignments from definitely-readonly things to possibly-readonly things.
I totally agree that we must prevent codes from libraries and/or node modules, as still today there's modules that doesn't include d.ts files and the DefinitelyTyped repo doesn't have 100% of updated d.ts files.
BUT as the main purpose was to do a compromise, let's add a compromise over the compromise, see the code below to understand why we should make the user more careful about mutating a readonly property when deriving a class.
TypeScript Version: 3.3.0-dev.201xxxxx
Search Terms: final readonly properties derived inherits
Code
abstract class Pet {
protected abstract race: string;
protected abstract sayMyRace(): void;
}
class Dog extends Pet {
protected readonly race: string = "Dog";
sayMyRace() {
console.log(this.race);
}
}
class NotADog extends Dog {
protected readonly race: string = "Robot";
}
const scooby = new Dog();
scooby.sayMyRace();
const marvin = new NotADog();
marvin.sayMyRace();
Expected behavior:
Error saying that race in NotADog cannot be modified as it was already declared in class Dog and we set the same attributes in both cases, meaning we do want to override a constant.
Actual behavior:
Property is overwritten, ignoring the readonly property from the already existing parent's property.
Playground Link:
here
Note that in the documentation the example shows an error when trying to mutate the property created in the class.
I'm not asking to put a check rule on every keyword in every line of code, but to accept that if a class have a readonly property, nobody can mutate it from outside, I'm not talking about the interfaces and/or declaration file to help to make the compromise, but the Typescript documentation state:
The easiest way to remember whether to use readonly or const is to ask whether you’re using it on a variable or a property. Variables use const whereas properties use readonly.
If a property with readonly is a variable with const, then we must keep this in line and prevent any mutation from a class property to another class property, at least that would allow to add a safety check in the code.
Also the documentation regarding the readonly properties in Interfaces state that
Some properties should only be modifiable when an object is first created.
If the team says that a mutation between an explicitly typed readonly property from a class to a subclass is normal as the compromise was done and that changing the rules would break the declaration files (even though two years had past)
So to sum up :
I open this issue as a bug because the documentation states that a readonly property is a constant variable, and that based on the few informations about it (still from the documentation), this is not the expected behavior.
I also considered the previous discussion and the compromise made to prevent any blocking backward compatibility, and asking only to use the readonly check rule from class to class by default, and allow more in a tsconfig rule later.
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.4k
- 平均合并
- 1 天 15 小时
- 30 天内合并 PR
- 106
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
microsoft/TypeScript 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 88/100
microsoft/TypeScript#64322 · 2 条评论 · 1 个 reaction · 已指派 2 人 ·
-
Possible Improvement
难度 2/5 1-3 小时 新手友好度 78/100
microsoft/TypeScript#64278 · 1 条评论 · 1 个 reaction ·
-
Docs
难度 2/5 1-3 小时 新手友好度 70/100
microsoft/TypeScript#64118 · 1 条评论 ·
-
难度 1/5 1 小时以内 新手友好度 88/100
microsoft/TypeScript#64094 ·
-
Docs
难度 2/5 1-3 小时 新手友好度 76/100
microsoft/TypeScript#63959 · 5 条评论 ·
查看 microsoft/TypeScript 的全部 Issue
相似的 Issue
-
Type/Bug
难度 2/5 1-3 小时 新手友好度 78/100
OpenNSW/nsw-srilanka#497 ·
-
难度 1/5 1 小时以内 新手友好度 92/100
milvus-io/birdwatcher#545 ·
-
kind/bug
难度 2/5 1-3 小时 新手友好度 88/100
kubernetes-sigs/prow#953 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 88/100
-
难度 2/5 1-3 小时 新手友好度 88/100
caddyserver/caddy#8046 ·