[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 ...
随机推荐
- struts标签include传参的问题
传一个常量过去居然为null, <s:include value="/biz/customer/corp/module/franchisemanageright/corpFranchi ...
- 运行HelloWorld.class是报错(错误: 找不到或无法加载主类 HelloWorld.class)
1.从毕业到现在工作了几个月了,每天都是在写一些js代码,感觉作为一个web程序员,java还是十分重要的,于是自己买了一本java书来边学边练习,然后发现自己连使用记事本来编写的HelloWorld ...
- DRF基类APIView提供的Request、Response和序列化器的综合使用
关于DRF基类APIView提供的Request和Response对象的作用,可以看我的另一篇博文:https://www.cnblogs.com/chichung/p/9939864.html 综合 ...
- django日志的设置
关于django的日志设置详细可以看下官方文档:https://yiyibooks.cn/xx/Django_1.11.6/topics/logging.html 示例: # 日志文件配置 LOGGI ...
- wxBot微信机器人框架(转)
原文:http://blog.csdn.net/tobacco5648/article/details/50722321 wxBot 是Python包装Web微信实现的微信机器人框架.可以很容易地实现 ...
- Cookie和session的简单理解和应用
一.COOKIE 1.http协议建立连接后,无法保持状态:但实际情况,网站和服务器要进行通讯,需要“保持状态”,因此cookie应运而生:浏览器登陆web服务器后, Web 服务器产生包含有关用户的 ...
- gcc与gdb的使用
1.gcc/g++编译过程: gcc/g++的编译格式: gcc [option][filename]... g++ [option][filename]... 1)processing:预处理过程, ...
- 11、Flask实战第11天:蓝图
蓝图的基本使用 之前我们写的代码都是集中在一个主程序文件里面.这样不利于分层解耦和维护.蓝图的作用就是让我们的flask项目更加模块化结构更加清晰,可以将相同模块的视图函数放在同一个蓝图下,同一个文件 ...
- Codeforces 702 D Road to Post Office
题目描述 Vasiliy has a car and he wants to get from home to the post office. The distance which he needs ...
- python3-开发面试题(python)6.22基础篇(1)
1.为什么学习Python? 1.语言排行榜 2.语言本身简洁,优美,功能超级强大的 3.跨平台 4.非常火爆的社区 5.用的公司的多 2.通过什么途径学习的Python? 某宝2.8就搞定了,跟着视 ...