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的更多相关文章

  1. [Effective Modern C++] Item 5. Prefer auto to explicit type declarations - 相对显式类型声明,更倾向使用auto

    条款5 相对显式类型声明,更倾向使用auto 基础知识 auto能大大方便变量的定义,可以表示仅由编译器知道的类型. template<typename It> void dwim(It ...

  2. [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 ...

  3. Go语言规格说明书 之 类型声明(Type declarations)

    go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,完整的介绍Go语 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. 一种封装Retrofit的方法,可以自动解析Gson,回避Method return type must not include a type variable or wildcard: retrofit2.Call<T>的问题

    封装目的:屏蔽底层实现,提供统一接口,并支持Gson自动转化 最初封装: //请求方法 interface RequestListener { interface PostListener { @PO ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 【bzoj3227】红黑树

    神TM的红黑树,其实本质上应该还是一种树dp的问题…… 一开始想了一个比较裸的树dp,后来发现还有更强的做法. 每个前端黑节点是看作一个物品,然后这就是很典型的树形dp的问题. 不过可以这么考虑,考虑 ...

  2. 2.jinja2

    1.jinja2模板介绍和查找路径 from flask import Flask, render_template import os # 之前提到过在渲染模板的时候,默认会从项目根目录下的temp ...

  3. crontab执行脚本失败问题

    如果脚本在终端执行正常,而在crontab里执行失败, 一般跟root用户无关,而是和系统变量有关,一般加上绝对路径执行java命令即可: /usr/java/jdk/bin/java xxx.jar

  4. k-密码

    链接:https://www.nowcoder.com/acm/contest/90/K来源:牛客网 题目描述 ZiZi登录各种账号的时候,总是会忘记密码,所以他把密码都记录在一个记事本上.其中第一个 ...

  5. sourceforge的FTP镜像

    https://www.mirrorservice.org/sites/ftp.sourceforge.net/

  6. String.Format,DateTime日期时间格式化

    DateTime dt = DateTime.Now;//2010年10月4日 17点05分            string str = "";            //st ...

  7. debug 英文翻译

    declaration of 'int a' shadows a parameter :函数参数里,已经有这两个名称的变量了,指定义了同名的参数,造成了隐藏. expected primary-exp ...

  8. POJ 3083 Children of the Candy Corn (DFS + BFS + 模拟)

    题目链接:http://poj.org/problem?id=3083 题意: 这里有一个w * h的迷宫,给你入口和出口,让你分别求以下三种情况时,到达出口的步数(总步数包括入口和出口): 第一种: ...

  9. [BZOJ 1305] 跳舞

    Link:https://www.lydsy.com/JudgeOnline/problem.php?id=1305 Solution: 发现res是否可行具有单调性,二分答案 容易看出每次check ...

  10. POJ 3537:Crosses and Crosses(Multi-Nim)

    [题目链接] http://poj.org/problem?id=3537 [题目大意] 在一个1*n的方格纸上下棋,谁先连三子谁就赢了,问必胜的是谁. [题解] 我们发现对于一个n规模的游戏.在i位 ...