[Typescript] 94. Hard - Get Optional
typescript get Hard optional 94
2023-09-14 08:59:11 时间
Implement the advanced util type GetOptional<T>
, which remains all the optional fields
For example
type I = GetOptional<{ foo: number, bar?: string }> // expected to be { bar?: string }
/* _____________ Your Code Here _____________ */
type GetOptional<T extends Record<PropertyKey, any>> = {
[Key in keyof T as T[Key] extends Required<T>[Key] ? never: Key]: T[Key]
};
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<GetOptional<{ foo: number; bar?: string }>, { bar?: string }>>,
Expect<Equal<GetOptional<{ foo: undefined; bar?: undefined }>, { bar?: undefined }>>,
]
相关文章
- 基于 Vue3、TypeScript、Vite2、Pinia 开源的后台管理框架
- typescript的泛型_泛型有什么用
- TypeScript(6)函数「建议收藏」
- Typescript教程_安装typescript
- 简单的贪吃蛇小游戏—TypeScript练习
- PHP CURL GET HTTPS报错SSL certificate problem: unable to get local issuer certificate
- typescript error TS2322: Type ‘Timeout‘ is not assignable to type ‘number‘.
- 让你更好使用 Typescript 的11个技巧
- TypeScript入门
- 开心档之TypeScript 联合类型
- Node.js脚手架开发完全指南「TypeScript版」
- TypeScript 2.0 与 AngularJS 2.0 的新动态
- TypeScript 4.4 RC 发布