[TypeScript] Deeply mark all the properties of a type as read-only in TypeScript
We will look at how we can use mapped types, conditional types, self-referencing types and the “infer” keyword to create a reusable generic type that traverses down the properties of an object and marks of all them as read-only. This is especially useful when used with Redux, to ensure our whole state tree is marked as read-only and immutable.
For example we have complex interface:
interface IRootState {
userId: string;
showCompletedOnly: boolean;
todoTypes: string[];
todos: ITodo[];
iconGrid: string[][];
} interface IEmail {
from: string;
to: string[];
body: string;
} interface ITodo {
isCompleted: boolean;
text: string;
linkedEmail: IEmail;
}
If we want to add readonly to all the props of IRootState. We want to do it automaticlly.
type DeepReadonlyObject<T> = { readonly [K in keyof T]: DeepReadonly<T[K]> }; type DeepReadonly<T> = T extends (infer E)[] ?
ReadonlyArray<DeepReadonlyObject<E>> :
T extends object ? DeepReadonlyObject<T> :
T;
type IReadonlyRootState = DeepReadonly<IRootState>;
[TypeScript] Deeply mark all the properties of a type as read-only in TypeScript的更多相关文章
- [Vue + TS] Use Properties in Vue Components Using @Prop Decorator with TypeScript
With properties we can follow a one-way parent→child flow communication between components. This les ...
- [TypeScript] Use the JavaScript “in” operator for automatic type inference in TypeScript
Sometimes we might want to make a function more generic by having it accept a union of different typ ...
- Checking Types Against the Real World in TypeScript
转自:https://www.olioapps.com/blog/checking-types-real-world-typescript/ This is a follow-up to Type-D ...
- TypeScript - Classes
简介 JavaScript语言基于函数和原型链继承机制的方式构建可重用的组件.这对于OO方面编程来说显得比较笨拙.在下一代的JavaScript标准ECMAScript 6为我们提供了基于class ...
- TypeScript中的怪语法
TypeScript中的怪语法 如何处理undefined 和 null undefined的含义是:一个变量没有初始化. null的含义是:一个变量的值是空. undefined 和 null 的最 ...
- Angular基础(三) TypeScript
一.模仿Reddit a) 运行ng new –ng4angular-reddit创建应用,从随书代码中复制样式文件,新建组件app-root,代码为: 界面可以看到了: b) 对于界面输入的数据,获 ...
- typescript枚举,类型推论,类型兼容性,高级类型,Symbols(学习笔记非干货)
枚举部分 Enumeration part 使用枚举我们可以定义一些有名字的数字常量. 枚举通过 enum关键字来定义. Using enumerations, we can define some ...
- Declaration Merging with TypeScript
原文:https://blog.oio.de/2014/03/21/declaration-merging-typescript/ Why might you need this? There can ...
- [转]TypeScript Quick start
本文转自:http://www.typescriptlang.org/docs/tutorial.html Quick start Get started with a simple TypeScri ...
随机推荐
- AWS Ubuntu部署EMQTT的小坑
Redis身份认证中的 is_superuser 表示不查检用户密码和ACL规则 如果是Ubuntu,集群设置节点名须改为:ubuntu@私有Ip,安全组最好相互开放所有端口 如果使用了ELB,EMQ ...
- elasticSearch script api
Package org.elasticsearch.script Support for running user provided scripts (in the request, in clust ...
- python基础(字符串常用、数字类型转换、基本运算符与流程控制)
一.字符串常用操作: #! /usr/bin/env python # -*- coding: utf-8 -*- # __author__ = "Z'N'Y" # Date: 2 ...
- Jquery实现全选和取消全选的方法
<input type="checkbox" id="all" />全选<br /> <input type="chec ...
- 提取windows用户明文密码
前段时间mimikatz热传,主要是因为可以直接提取当前登录用户明文密码. 其实,有个更厉害的神器,无需那么多命令操作,一个命令搞定: C:\>wce -w WCE v1.3beta (Wind ...
- 51nod 1265 四点共面【计算几何+线性代数】
1265 四点共面 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出三维空间上的四个点(点与点的位置均不相同),判断这4个点是否在同一个平面内(4点共 ...
- centos中pyenv安装
1.先安装git yum install git -y 2.克隆pyenv到本地 git clone git://github.com/yyuu/pyenv.git .pyenv 或自动安装 curl ...
- 【Android】 HttpClient 发送REST请求
直接po代码吧,第一个是一个枚举类型的类,是四种rest http请求,get/post/put/delete: public enum HttpRequestMethod { HttpGet { @ ...
- oracle delete all index own by table
BEGIN FOR ind IN (SELECT index_name FROM user_indexes WHERE table_name = '') LOOP execute immediate ...
- bzoj 5347: 冒泡排序
考虑到最后a[i]都要等于i,并且每个 a[i] < i 的a[i] 一轮最多向前走一次,所以局数至少是 max{ i - a[i] }. 又因为对于a[i] < i来说,一轮不动意味着 ...