简体   繁体   English

在接口和/或类中强制执行只读

[英]Enforcing Readonly in Interfaces and/or Classes

I'm working on a class that I'm using as a Model, and I want to make sure that the data is readonly.我正在研究一个用作模型的类,我想确保数据是只读的。 It's simple enough on its own, but it seems to get tricky when the data is nested.它本身很简单,但是当数据嵌套时似乎变得棘手。 Let me give and example of my code to make this more clear:让我举例说明我的代码,以使其更清楚:

export class MyModel {
  readonly One: IOne;
  readonly Two: ITwo;
  readonly Three: IThree;

  constructor() {
    this.One.A = "foo";
    this.One.B = "bar";
    this.Two.C = "totally";
    this.Two.D = "utterly";
    this.Three.E = "completely";
    this.Three.F = "messed up"
  }
}

interface IOne {
  A: string;
  B: string;
}

interface ITwo {
  C: string;
  D: string;
}

interface IThree {
  E: string;
  F: string;
}

Then in a component:然后在一个组件中:

import { MyModel } from './my.model';

export MyComponent {
  
  dataB:string;
  dataD:string;
  dataE:string;

  constructor(
    private mymodel: MyModel
  ){
    this.dataB = mymodel.One.B; // works as expected
    mymodel.Two = ""; // gives error since it is readonly, as expected
    mymodel.Three.F = "something else"; // works, but I want this to be readonly as well
  }

}

The idea is that I can keep my data in places where they make the most sense but also allow my IDE to give hints of what is available as I type mymodel.One.B.这个想法是我可以将我的数据保存在它们最有意义的地方,但也允许我的 IDE 在我输入 mymodel.One.B 时给出可用的提示。 But, I need to have the properties (not sure I'm using the right term here) from the Interfaces to also be readonly.但是,我需要将接口中的属性(不确定我在这里使用的术语是否正确)也设为只读。

If I add readonly to the Interfaces, I of course get an error in MyModel because I cannot set the values of something that is readonly.如果我将只读添加到接口中,我当然会在 MyModel 中收到错误,因为我无法设置只读内容的值。 To get around this, I tried creating other classes that implement those Interfaces, set readonly in the classes, then use the classes instead of the interfaces, it does not give the error, but they are still not prevented from assigning values in MyComponent.为了解决这个问题,我尝试创建其他实现这些接口的类,在类中设置只读,然后使用类而不是接口,它没有给出错误,但仍然没有阻止它们在 MyComponent 中分配值。 Example:例子:

export class MyModel {
  readonly One: One;
  readonly Two: Two;
  readonly Three: Three;

  constructor() {}
}

class One implements IOne {
  readonly A = "foo";
  readonly B = "bar";
}

class Two implements ITwo {
  readonly C = "totally";
  readonly D = "utterly";
}

class Three implements IThree {
  readonly E = "completely";
  readonly F = "messed up";
}

interface IOne {
  A: string;
  B: string;
}

interface ITwo {
  C: string;
  D: string;
}

interface IThree {
  E: string;
  F: string;
}

The result in MyComponent is the same. MyComponent 中的结果是相同的。 How do I enforce readonly for A, B, C, etc.?如何对 A、B、C 等强制执行只读?

I found the proper way to make this work.我找到了使这项工作的正确方法。

I hadn't realized it, but the way I have MyModel written throws an error the moment MyComponent instantiates it because mymodel.A is undefined.我没有意识到这一点,但是我编写 MyModel 的方式会在 MyComponent 实例化它的那一刻引发错误,因为 mymodel.A 未定义。 The trick was to set values outside of the constructor, which also led me to the solution for my original question:诀窍是在构造函数之外设置值,这也使我找到了原始问题的解决方案:

export class MyModel {
  readonly One: IOne = {
    A: "foo",
    B: "bar"
  }

  constructor(){}
}

interface IOne {
  readonly A: string,
  readonly B: string
}

This produced the proper error when trying to assign a value to mymodel.One.A in MyComponent.这在尝试为 MyComponent 中的 mymodel.One.A 赋值时产生了正确的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM