跳到主要内容

ObjectEntries

介绍

从类型T中, 筛选属性类型不能赋值为'U'的属性集合.

ts
interface Model {
name: string;
age: number;
locations: string[] | null;
}
type modelEntries = ObjectEntries<Model> // ['name', string] | ['age', number] | ['locations', string[] | null];
ts
interface Model {
name: string;
age: number;
locations: string[] | null;
}
type modelEntries = ObjectEntries<Model> // ['name', string] | ['age', number] | ['locations', string[] | null];
View on GitHub

起点

ts
/* _____________ Your Code Here _____________ */
 
type ObjectEntries<T> = any
 
/* _____________ Test Cases _____________ */
 
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>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<ObjectEntries<Partial<Model>>, ModelEntries>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<ObjectEntries<{ key?: undefined }>, ['key', undefined]>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<ObjectEntries<{ key: undefined }>, ['key', undefined]>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<ObjectEntries<{ key: string | undefined }>, ['key', string | undefined]>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
ts
/* _____________ Your Code Here _____________ */
 
type ObjectEntries<T> = any
 
/* _____________ Test Cases _____________ */
 
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>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<ObjectEntries<Partial<Model>>, ModelEntries>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<ObjectEntries<{ key?: undefined }>, ['key', undefined]>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<ObjectEntries<{ key: undefined }>, ['key', undefined]>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<ObjectEntries<{ key: string | undefined }>, ['key', string | undefined]>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
take the challenge

解决方案

Spoiler warning // Click to reveal answer
ts
/**
* Required<T> 会将可选属性的值undefined变成never
*/
 
// most popular
type ObjectEntries<T,U = Required<T>> = {
[K in keyof U]:[K,U[K]]
}[keyof U]
 
 
// 因为要解决值变成never的问题, 所以多了个条件判断
type ObjectEntries2<T, U = Required<T>> = {
[K in keyof U]: [K, U[K] extends never ? undefined : U[K]]
}[keyof U]
 
ts
/**
* Required<T> 会将可选属性的值undefined变成never
*/
 
// most popular
type ObjectEntries<T,U = Required<T>> = {
[K in keyof U]:[K,U[K]]
}[keyof U]
 
 
// 因为要解决值变成never的问题, 所以多了个条件判断
type ObjectEntries2<T, U = Required<T>> = {
[K in keyof U]: [K, U[K] extends never ? undefined : U[K]]
}[keyof U]
 
view more solutions