[Typescript] “Required” Mapped Type, +/- Modifiers
typescript type required mapped
2023-09-14 08:59:13 时间
For example we have interface:
interface Person { name: string, age?: number }
'age' is an optional prop.
// will have an error since person.age is used function printAge(person) { return `${person.name} is ${person.age}` } const person = { name: "wan' } printAge(person)
Required type:
type MyRequired<T> = { [P inkeyof T]-?: T[P] }
'-?': remove optional modifier so it become required prop.
function printAge(person: MyRequired<Person>) { return `${person.name} is ${person.age}` } const person: MyRequired<Person> = { name: "wan', age: 23 } printAge(person)
or can use built-in:
function printAge(person: Required<Person>) { return `${person.name} is ${person.age}` } const person: Required<Person> = { name: "wan', age: 23 } printAge(person)
相关文章
- javascript typescript_typescript python
- 计算时间差工具类(TypeScript/JavaScript)
- java设置content type_Response Content Type设置[通俗易懂]
- typescript 多继承_说明类的继承和多态有什么作用
- TypeScript(3)基础类型[通俗易懂]
- ESLint: Typescript + React + TSX 集成
- Typescript: Getting Started
- TypeScript学习笔记(二)—— TypeScript基础
- TypeScript 编译性能优化:Project Reference
- Vue 3 + Typescript + Vite 基个人网站二维码登录功能
- Vue 3 + Typescript + Vite 基于jsplumb流程设计和流程流转
- TypeScript 中的 any、unknown、never 和 void
- TypeScript泛型应用
- TypeScript | 笔记
- 软件开发入门教程网之TypeScript 声明文件
- 用TodoList实例告诉你怎么在项目中使用TypeScript
- TypeScript 新语法 satisfies:用声明还是用推导?这是个问题
- (十四)与 TypeScript 集成
- 从0到1开启一个全新的TypeScript项目
- ORA-22319: type attribute information altered in ALTER TYPE ORACLE 报错 故障修复 远程处理
- ORA-22343: Compilation error for type invalidated by ALTER TYPE ORACLE 报错 故障修复 远程处理
- ORA-24025: invalid value string, QUEUE_PAYLOAD_TYPE should be RAW or an object type ORACLE 报错 故障修复 远程处理
- Oracle中定义Type的概念及其用法(oracle定义type)
- Linux下使用Type命令掌握文件说明(linux中 type)