1 Star 1 Fork 0

wangzichu/ts-challenges

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
2946.ObjectEntries.ts 2.15 KB
一键复制 编辑 原始数据 按行查看 历史
王子初 提交于 2023-04-20 18:23 . 第二次提交
/*
* @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
* @Date: 2023-04-13 13:45:05
* @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
* @LastEditTime: 2023-04-17 10:37:48
* @FilePath: /ts-challenges/ 2946.ObjectEntries.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
/*
2946 - ObjectEntries
-------
by jiangshan (@jiangshanmeta) #medium #object
### Question
Implement the type version of ```Object.entries```
For example
```typescript
interface Model {
name: string;
age: number;
locations: string[] | null;
}
type modelEntries = ObjectEntries<Model> // ['name', string] | ['age', number] | ['locations', string[] | null];
```
> View on GitHub: https://tsch.js.org/2946
*/
/* _____________ Your Code Here _____________ */
type ObjectEntries<T> = NonNullable<{
[key in keyof T]: [key,T[key] extends (infer R | undefined)?R:T[key]]
}[keyof T]>
// type ObjectEntries<T> = Exclude<{[K in keyof T]: [K, T[K]]}[keyof T],undefined>
type test = ObjectEntries<Partial<Model>>
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
interface Model {
name: string
age: number
locations: string[] | null
}
type ModelEntries = ['name', string] | ['age', number] | ['locations', string[] | null]
type cases = [
Expect<Equal<ObjectEntries<Model>, ModelEntries>>,
Expect<Equal<ObjectEntries<Partial<Model>>, ModelEntries>>,
Expect<Equal<ObjectEntries<{ key?: undefined }>, ['key', undefined]>>,
Expect<Equal<ObjectEntries<{ key: undefined }>, ['key', undefined]>>,
]
/* _____________ Further Steps _____________ */
/*
> Share your solutions: https://tsch.js.org/2946/answer
> View solutions: https://tsch.js.org/2946/solutions
> More Challenges: https://tsch.js.org
*/
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wang-zichu/ts-challenges.git
git@gitee.com:wang-zichu/ts-challenges.git
wang-zichu
ts-challenges
ts-challenges
master

搜索帮助