[TypeScript] Create Explicit and Readable Type Declarations with TypeScript mapped Type Modifiers
2024-08-27 15:41:12
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:
interface IPet {
name: string;
age: number;
favoritePark?: string
}
There is two required props and one favoriatePark as optional prop.
From TypeScirpt 2.8, we are able to gereate a new interface based on existing one, and add or remove props:
For example we want to remove all the optional props, we can use '-':
interface IPetRequired {
[K in keyof IPET]-?: IPet[K]
}
'-': remove
'?': optional
'-?': remove optional
We can also use '+' to indicate what we have added:
type ReadonlyPet = {
+readonly [K in keyof IPet]?: IPet[K]
}
Here we added readonly type.
最新文章
- 21、ASP.NET MVC入门到精通——ASP.NET MVC4优化
- JSP之->;初识JSP
- springMVC注解启用及优化
- C#预处理器指令 ,你造吗??? (●'◡'●)
- UIControl事件
- [转载] 一致性问题和Raft一致性算法
- 理解js闭包(二)
- vssettings 的备份和导入
- iTunes无法验证服务器“s.mzstatic.com.”的身份
- codevs 1183 泥泞的道路 (二分+SPFA+差分约束)
- UML建模之活动图介绍(Activity Diagram)
- c#关于EXCEL导出数据库的做法
- Android- assent和raw的区别
- postman接口测试系列:接口参数化和参数的传递
- salesforce零基础学习(八十五)streaming api 简单使用(接近实时获取你需要跟踪的数据的更新消息状态)
- 【CJOJ1372】【洛谷2730】【USACO 3.2.5】魔板
- SpringBoot+Mybatis+ Druid+PageHelper 实现多数据源并分页
- Java面向对象概述及三大特征(封装,继承和多态)
- mvn打包时添加日期参数
- Python print函数用法,print 格式化输出