Typescript & readonly property

https://www.typescriptlang.org/docs/handbook/classes.html#readonly-modifier

class Octopus {
readonly name: string;
readonly numberOfLegs: number = 8;
constructor (theName: string) {
this.name = theName;
}
}
let dad = new Octopus("Man with the 8 strong legs");
dad.name = "Man with the 3-piece suit"; // error! name is readonly.

https://www.typescriptlang.org/docs/handbook/utility-types.html#readonlyt

interface Todo {
title: string;
} const todo: Readonly<Todo> = {
title: 'Delete inactive users',
}; todo.title = 'Hello'; // Error: cannot reassign a readonly property

// Object.freeze

function freeze<T>(obj: T): Readonly<T>;

Object.freeze

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze

const

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const

refs

https://www.tutorialsteacher.com/typescript/typescript-readonly

class Employee {
readonly empCode: number;
empName: string; constructor(code: number, name: string) {
this.empCode = code;
this.empName = name;
}
}
let emp = new Employee(10, "John");
emp.empCode = 20; //Compiler Error
emp.empName = 'Bill'; //Compiler Error

https://mariusschulz.com/blog/read-only-properties-in-typescript



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


Typescript & readonly property的更多相关文章

  1. Computed read-only property vs function in Swift

    In the Introduction to Swift WWDC session, a read-only property description is demonstrated: class V ...

  2. Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{...

    转载请标明出处:https://www.cnblogs.com/tangZH/p/10764568.html 在做多渠道打包的时候出现了这个错误,在高版本的gradle出现. 具体错误为:Cannot ...

  3. TypeScript 错误property does not exist on type Object

    TypeScript 错误property does not exist on type Object 在TypeScript中如果按JS的方式去获取对象属性,有时会提示形如Property 'val ...

  4. Gradle sync failed: Cannot set the value of read-only property 'outputFile'

    错误 Gradle sync failed: Cannot set the value of read-only property 'outputFile' 原因 gradle打包,自定义apk名称代 ...

  5. 【bug】【yii】配置log时,报错 Setting read-only property: yii\web\Application::log

    Setting read-only property: yii\web\Application::log 配置放在了 components 外面,应该放在里面

  6. React报错之Cannot assign to 'current' because it is a read-only property

    正文从这开始~ 总览 当我们用一个null值初始化一个ref,但在其类型中不包括null时,就会发生"Cannot assign to 'current' because it is a r ...

  7. TypeScript 真香系列——接口篇

    接口带来了什么好处 好处One —— 过去我们写 JavaScript JavaScript 中定义一个函数,用来获取一个用户的姓名和年龄的字符串: const getUserInfo = funct ...

  8. 《三》大话 Typescript 接口

    > 前言: 本文章为 TypeScript 系列文章. 旨在利用碎片时间快速入门 Typescript. 或重新温故 Typescript 查漏补缺.在官方 api 的基础上, 加上一些日常使用 ...

  9. TypeScript基础以及在Vue中的应用

    TypeScript推出已经很长时间了,在Angular项目中开发比较普遍,随着Vue 3.0的即将推出,TypeScript在Vue项目中使用也即将成为很大的趋势,笔者也是最近才开始研究如何在Vue ...

随机推荐

  1. 提供个HDFS的目录的路径,对该目录进行创建和删除操作。创建目录时,如果目录 文件所在目录不存在则自动创建相应目录;删除目录时,由用户指定当该目录不为空时是否还删 除该目录

    import java.io.IOException; import java.util.Scanner; import org.apache.hadoop.fs.*; public class G_ ...

  2. Go Code Review Comments

    Go Code Review Comments https://golang.org/wiki/CodeReviewComments

  3. JVM笔记 -- Java跨平台和JVM跨语言

    学习JVM的重要性 从上层应用程序到底层操作系统,到底有哪些东西? 平时开发的应用程序主要基于各种框架,譬如Spring,SpringMVC,Mybatis,而各种框架又是基于Java API来实现的 ...

  4. 洛谷 p3391

    题目背景 这是一道经典的Splay模板题--文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...

  5. MySql数据表设计,索引优化,SQL优化,其他数据库

    MySql数据表设计,索引优化,SQL优化,其他数据库 1.数据表设计 1.1数据类型 1.2避免空值 1.3text类型优化 2.索引优化 2.1索引分类 2.2索引优化 3.SQL优化 3.1分批 ...

  6. ESRI,空间数据处理,WKT,GeoJson

    ESRI,空间数据处理,WKT,GeoJson 一.WKT 二.GeoJson 三.WKT转GeoJson 四.GeoJson 转 WKT 一.WKT WKT(well-known text)是一种文 ...

  7. (9)Linux的哲学思想及文件概念

    一.Linux的哲学思想 1. 一切皆文件 把几乎所有资源统统抽象为文件形式,包括硬件设备,甚至通信接口等,便于统一管理和定义: 对文件的操作有:open,read,write,close,delet ...

  8. docker(12)使用Dockerfile创建jenkins+python3+pytest环境

    前言 之前我们用docker手动安装了jenkins环境,在jenkins中又安装了python3环境和各种安装包,如果我们想要在其他3台机器上安装,又是重复操作,重复劳动,那会显得很low,这里可以 ...

  9. HanLP 下载和配置

    方式一.Maven 为了方便用户,特提供内置了数据包的Portable版,只需在pom.xml加入: <dependency> <groupId>com.hankcs</ ...

  10. BZOJ2555 SubString【SAM + Link Cut Tree】

    BZOJ2555. SubString 要求在线询问一个串在原串中出现的次数,并且可以在原串末尾添加字符串 如果没有修改的话,考虑建出\(parent\)树之后统计每个\(endpos\)节点的\(r ...