[TypeScript] Create Explicit and Readable Type Declarations with TypeScript mapped Type Modifiers(代码
敢说敢作敢为, 无怨无恨无悔。这篇文章主要讲述[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] }
【[TypeScript] Create Explicit and Readable Type Declarations with TypeScript mapped Type Modifiers(代码】Here we added readonly type.
推荐阅读
- Android Studio教程06-布局,监听器以及基本控件
- 安卓机数字键盘弹出页面被挡问题
- 安卓目录
- android:padding和android:margin的区别
- Flutter - 退出App
- React Native - Android 环境的安装
- android中RecyclerView控件实现长按弹出PopupMenu菜单功能
- Android 高速录像
- Android-Java-封装