[TypeScript] Make Properties and Index Signatures Readonly in TypeScript

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[TypeScript] Make Properties and Index Signatures Readonly in TypeScript相关的知识,希望对你有一定的参考价值。

TypeScript 2.0 introduced the readonly modifier which can be added to a property or index signature declaration. It helps prevent against unintended property assignments. This lesson gives various use cases for readonly and shows what the generated javascript code looks like.

 

Normal use case for ‘readonly‘:

interface User {
   readonly id: nunber;  
   name: string
} 

class User {

  readonly id: number;
   name: string; 
  constructor(
      id: number, name: string
  ) {
      this.id = id;
      this.name = name;
  }
}

 

Make a array readonly:

const level: ReadonlyArray<string> = [
 master,
 beginner
];

 

以上是关于[TypeScript] Make Properties and Index Signatures Readonly in TypeScript的主要内容,如果未能解决你的问题,请参考以下文章

python - class propert应用

-Dmaven.multiModuleProjectDirectory system propert

扩展嵌套 typescript 接口

类的 propert,classmethod,ataticmethod 方法 与 多态

方法未作为 TypeScript 编译的一部分附加/检测

Mybatismybatis查询报错org.apache.ibatis.reflection.ReflectionException: There is no getter for propert(代