首页 > 其他 > 详细

[TypeScript] Make Properties and Index Signatures Readonly in TypeScript

时间:2017-11-09 21:31:35      阅读:177      评论:0      收藏:0      [点我收藏+]

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

原文:http://www.cnblogs.com/Answer1215/p/7811475.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!