[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:
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.
[TypeScript] Create Explicit and Readable Type Declarations with TypeScript mapped Type Modifiers的更多相关文章
- [Effective Modern C++] Item 5. Prefer auto to explicit type declarations - 相对显式类型声明,更倾向使用auto
条款5 相对显式类型声明,更倾向使用auto 基础知识 auto能大大方便变量的定义,可以表示仅由编译器知道的类型. template<typename It> void dwim(It ...
- [TypeScript] Create a fluent API using TypeScript classes
You can create an easy to chain API using TypeScript classes. Learn about the thisreturn type annota ...
- Go语言规格说明书 之 类型声明(Type declarations)
go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,完整的介绍Go语 ...
- [TypeScript] Infer the Return Type of a Generic Function Type Parameter
When working with conditionals types, within the “extends” expression, we can use the “infer” keywor ...
- [TypeScript] Union Types and Type Aliases in TypeScript
Sometimes we want our function arguments to be able to accept more than 1 type; e.g. a string or an ...
- [TypeScript] Restrict null and undefined via Non-Nullable-Types in TypeScript
This lesson introduces the --strictNullChecks compiler option and explains how non-nullable types di ...
- 一种封装Retrofit的方法,可以自动解析Gson,回避Method return type must not include a type variable or wildcard: retrofit2.Call<T>的问题
封装目的:屏蔽底层实现,提供统一接口,并支持Gson自动转化 最初封装: //请求方法 interface RequestListener { interface PostListener { @PO ...
- Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2020-02-06'; nested exception is java.lang.IllegalArgumentException]解决
今天做springbook项目前端输入日期传到数据库保存报了一下错误 Whitelabel Error Page This application has no explicit mapping fo ...
- The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
刚刚有在程序中,传递一个空值至MS SQL Server数据库,这个值的数据类型为DATETIME执行时,它却发生了如标题提示的异常:The conversion of a varchar data ...
随机推荐
- OSI和TCP/IP的对比+IP地址分类
一.OSI和TCP/IP对比 二.IP地址分类 A类私有IP地址:10.0.0.0-10.255.255.255B类私有IP地址:172.16.0.0-172.31.255.255C类私有IP地址:1 ...
- opencv第四章
1.载入一个带有有趣纹理的图像,使用cvSmooth()函数以多种方法平滑图像,参数为smoothtype = CV_GAUSSIAN. a.使用对称的平滑的平滑窗口,大小依次是3x3,5x5,9x9 ...
- Selenium2+python自动化46-js解决click失效问题【转载】
前言 有时候元素明明已经找到了,运行也没报错,点击后页面没任何反应.这种问题遇到了,是比较头疼的,因为没任何报错,只是click事件失效了. 本篇用2种方法解决这种诡异的点击事件失效问题 一.遇到的问 ...
- 利用ipv6技术,废旧笔记本变成互联网server
如果你家的路由器已经get到了ipv6地址,并且你家的电脑也获取了有效的ipv6地址,在广域网的设备可以访问到.那恭喜你,再配合我这个ddns,你可以完美地把你家的电脑当服务器使用. 1.确保你家的宽 ...
- 模板—字符串—Manacher
模板—字符串—Manacher Code: #include <cstdio> #include <cstring> #include <algorithm> us ...
- NOIP2011选择客栈
n家客栈,1~n编号,每家按照某一种色调装饰,共k种,每家客栈都设有咖啡店,每家咖啡店均有各自的最低消费两位游客,要求住在颜色相同,且不是同一个客栈,在两人的客栈间选择咖啡店(包括他们住的客栈),要求 ...
- Lightoj-1356 Prime Independence(质因子分解)(Hopcroft-Karp优化的最大匹配)
题意: 找出一个集合中的最大独立集,任意两数字之间不能是素数倍数的关系. 思路: 最大独立集,必然是二分图. 最大数字50w,考虑对每个数质因子分解,然后枚举所有除去一个质因子后的数是否存在,存在则建 ...
- [POJ1801]Formula Racing(模拟)
Formula Racing Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 289 Accepted: 77 Descr ...
- 【贪心】【multiset】 Codeforces Round #401 (Div. 2) B. Game of Credit Cards
对第一个人的排序,然后从小到大处理,对第一个人的每枚卡片,从第二个人的卡片中选择一个大于等于它的最小的,否则选择一个当前剩下的最小的,这样可以保证负场最少. 如果选择的改成大于它的最小的,就可以保证胜 ...
- [JOISC2017]細長い屋敷
题目大意: $n(n\le5\times10^5)$个房间排成一排,相邻两个房间之间有一扇门,第$i$个房间和第$i+1$个房间之间的门可以用第$c_i$种钥匙打开(可能有多个门可以用同一种钥匙打开) ...