[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 ...
随机推荐
- 3.orm之peewee
peewee是一款orm框架,为什么选择peewee,是因为它比较简单和Django比较类似,而且还有一个async-peewee,可以进行异步化. 如何定义model和生成表 ''' 我们要定义两张 ...
- JavaSript中数组方法是否对原数组产生影响
JavaScript中数组方法有很多.某次面试被问到,concat()方法会对影响到原数组吗.当时记得不牢,犹豫地说"会吧...".于是决定总结一下哪些数组方法会对原数组产生影响. ...
- python unittest 快速入门
import unittest def add(x, y): return x + y class TestLearning(unittest.TestCase): def setUp(self): ...
- 获取mac地址和IP地址方式
第一种 public class OperateMAC{ public static string GetMacByWMI() { string MacAddr = null; //Managemen ...
- 【python】抄写大神的糗事百科代码
照着静觅大神的博客学习,原文在这:http://cuiqingcai.com/990.html 划重点: 1. str.strip() strip函数会把字符串的前后多余的空白字符去掉 2. resp ...
- docker从零开始 存储(二)volumes 挂载
使用volumes 卷是保存Docker容器生成和使用的数据的首选机制.mount binds依赖于主机的目录结构,而卷完全由Docker管理.卷绑定安装有几个优点: 与绑定装入相比,卷更易于备份或迁 ...
- PHP无限极分类详谈
当你学习php无限极分类的时候,大家都觉得一个字“难”我也觉得很难,所以,现在都还在看,因为工作要用到,所以,就必须得研究研究. 到网上一搜php无限极分类,很多,但好多都是一个,并且,写的很乱,代码 ...
- [BZOJ1494][NOI2007]生成树计数 状压dp 并查集
1494: [NOI2007]生成树计数 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 793 Solved: 451[Submit][Status][ ...
- NetTcpBinding 类nettcpbinding的属性和方法
一种适合于跨计算机通信的安全可靠的绑定. 继承层次结构 System.Object System.ServiceModel.Channels.Binding System.Servi ...
- CF A.Mishka and Contest【双指针/模拟】
[链接]:CF/4892 [题意]: 一个人解决n个问题,这个问题的值比k小, 每次只能解决最左边的或者最右边的问题 解决了就消失了.问这个人能解决多少个问题. [代码]: #include<b ...