[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‘
];
时间: 2024-10-17 16:20:10

[TypeScript] Make Properties and Index Signatures Readonly in TypeScript的相关文章

[TypeScript] Query Properties with keyof and Lookup Types in TypeScript

The keyof operator produces a union type of all known, public property names of a given type. You can use it together with lookup types (aka indexed access types) to statically model dynamic property access in the type system. interface Todo { id: nu

[TypeScript] Type check JavaScript files using JSDoc and Typescript 2.5

Typescript 2.5 adds JSDoc type assertion support for javascript file via ts-check service. First of all, you should make sure you have [email protected] install: sudo npm i -g [email protected] Then add @ts-check to the top of js file: // @ts-check T

[TypeScript] Create Explicit and Readable Type Declarations with TypeScript mapped Type Modifiers

Using the optional “+” sign together with mapped type modifiers, we can create more explicit and readable type declarations. We can also use the “-” (minus) sign to remove optional declarations from properties. For example, we have an interface: inte

[TypeScript] Restrict null and undefined via Non-Nullable-Types in TypeScript

This lesson introduces the --strictNullChecks compiler option and explains how non-nullable types differ from nullable types. It also illustrates how you can write safer code by being explicit about null and undefined in the type system. First of all

TypeScript系列1-1.5版本新特性

1. 简介 随着PC端快速向移动端迁移,移动(体验)优先的概念也越来越响.由于ReactJS目前移动端仅仅支持iOS,因此移动端Web开发框架只能选择: AngularJS/Angula2 + Ionic框架 + Cordova.想要学习好Angula2以及阅读其代码, 就必须了解和学习TypeScript,也因此需要学习好ES6以及Web Component.近期将开始学习TypeScript语言. 下面先看看TypeScript语言的发展: 鉴于JavaScript这种脚本语言很难应用于大规

TypeScript 参数属性

假设类中创建的 readonly 类型的属性,该类型的属性只能在声明处或构造器中进行初始化. class Octopus { readonly name: string; readonly numberOfLegs: number = 8; constructor (theName: string) { this.name = theName; } } 为了初始化 name 属性,不得不在构造器中声明另一个入参 theName.这显得冗余. TypeScript 提供了在构造器上同时完成属性的声

TypeScript入门五:TypeScript的接口

TypeScript接口的基本使用 TypeScript函数类型接口 TypeScript可索引类型接口 TypeScript类类型接口 TypeScript接口与继承 一.TypeScript接口的基本使用 1.1定义TypeScript接口的指令(interface) 接口让我们想到的第一个短语就是(API).比如日常我们所说的调用某某程序的API一般都是跨应用的接口,如别的网站的或者APP的接口:还有我们每天都是用的编程语言的指令和内置的方法及属性,这些可以叫做编程语言的接口:还有令我们既

Vue3都要上的TypeScript之工程实践

0. 前言 怎么上... 咳咳,大家别想歪,这是一篇纯技♂术文章. 0.1 Why TypeScript 什么?尤大要把Vue 3.0全部改成用Typescript来写?这不是逗我吗,那我是不是要用TypeScript来写Vue应用了? 好吧,Vue3.0可能最快也要19年年末才出来,Vue3.0是会对Ts使用者更友好,而不是只能用ts了,尤大使用ts的原因也是因为ts的静态类型检测以及ts的表现比flow越来越好了.自从巨硬大步迈向开源,前端圈子多了很多新工具比如VS Code.TypeScr

搭建typescript开发环境最详细的全过程

本文<搭建typescript开发环境最详细的全过程>的源代码在 https://github.com/lingsbb/ts_demo/ 下载. 搭建typescript开发示例https://github.com/Microsoft/TypeScriptSamples typescript案例https://www.tslang.cn/samples/index.html 安装git:http://git-scm.com/download下安装git 安装node:https://nodej