type Componse<T> = {
[k in keyof T]: T[k]
}
type PartialByKeys<T, K extends keyof T = keyof T> = Componse<Omit<T, K> & {
[k in K]?: T[k]
}>
Solution by wendao-liu #35051
type PartialByKeys<T, K extends PropertyKey = keyof T> = { [P in keyof (Omit<T, K> & Partial<Pick<T, keyof T & K>>)]: T[P] }
Solution by devshinthant #34930
type IntersectionObj<T> = {
[P in keyof T]: T[P]
}
type PartialByKeys<T, K extends PropertyKey = keyof T> = IntersectionObj<{
[P in keyof T as P extends K ? P : never]?: T[P]
} & {
[P in keyof Omit<T, K>]: T[P]
}>
Solution by jzllove9 #34765
// your answers
type PartialByKeys<T, K extends keyof T = keyof T> = Omit<{
[key in keyof T as key extends K ? never: key]: T[key];
} & {
[key in keyof T as key extends K ? key: never]?: T[key];
}, never>
Solution by AndreGeng #34761
// your answers
type Intersection<T> = {
[K in keyof T]: T[K]
}
type PartialByKeys<T, K extends keyof T =keyof T> = Intersection<{
[X in Exclude<keyof T, K>]: T[X]
} & {
[X in K]?: T[X]
}>
Solution by gobielJonathan #34527
type PartialByKeys<T, K extends keyof T = never> = [K] extends [never] ? Partial<T> : Obj<{
[P in keyof T as P extends K ? P : never]?: T[P]
} & Omit<T, K>>
Solution by ouzexi #34049
type PartialByKeys<T, K extends keyof T = keyof T> =
Partial<Pick<T,K>> & Omit<T,K> extends infer G
? Pick<G,keyof G>:never
这样就行了
Solution by xdrlmm #33948
// 你的答案
type PartialByKeys<T, K extends string | number | symbol = keyof T> = { [ i in keyof PartialByKeys2<T, K> ] : PartialByKeys2<T, K>[i] }
type s = PartialByKeys
type PartialByKeys2<T, K extends string | number | symbol> = { [i in keyof T as i extends K ? never : i]: T[i] } & { [i in K]?: K extends keyof T ? T[i] : never } why is wrong?
Solution by zhangshuaiqiang11 #33882
type PartialByKeys<T, K extends keyof T = keyof T> = Omit<{
[key in Extract<keyof T,K>]?: T[key]
} & {
[key in Exclude<keyof T,K>]: T[key]
}, never>
Solution by kana-chan #33177
type PartialByKeys<T, K extends keyof T = keyof T> = Omit<Partial<Pick<T, K>> & Omit<T, K> , never>
Solution by iwebexpert #32590
type Cast<T> = { [K in keyof T]: T[K] }
type PartialByKeys<T extends object, K extends keyof T = keyof T> = Cast<{ [P in keyof T as P extends K ? P : never]?: T[P]} & Omit<T, K>>
억지로 value의 undefined 타입이 제거되도록 Cast 타입을 사용했지만 사용하지 않는 쪽이 더 나은거 같다. 문제가 조금 옛날 것인거 같다.
Solution by dev-hobin #32495
type MakePartial<T> = {
[P in keyof T]?: T[P]
}
type Flatten<T> = {
[K in keyof T]: T[K]
}
type PartialByKeys<T extends {}, K extends keyof T = keyof T> = [K] extends [''] ? MakePartial<T> : Flatten<Omit<T, K> & MakePartial<Pick<T, K>>>
Solution by keyurparalkar #32328
type PartialByKeys<T, U extends keyof T = keyof T> = Omit<Partial<Pick<T, U>> & Omit<T, U>, never>;
上面的版本和下面的版本是等价的,仅仅使用 Utility Type 的方法更简洁
type MergeType<O> = {
[P in keyof O]: O[P];
};
type PartialByKeys<T, K extends keyof T = keyof T> = MergeType<
{ [R in keyof T as R extends K ? R : never]?: T[R] } & Omit<T, K>
>;
其中
Omit<T,never>
的效果和 type MergeType<O>
效果是的。Partial<Pick<T, U>>
和 { [R in keyof T as R extends K ? R : never]?: T[R] }
效果是等价的Solution by vangie #32172
// your answers
type PartialByKeys<T, U> = { [K in keyof T as K extends U ? U : never]?: T[K] } & { [K in keyof T as K extends U ? never : K]: T[K] } extends infer I ? { [K in keyof I]: I[K] } : never
Solution by sunvictor #31650
// your answers
type Duplicate<T> = { [P in keyof T]: T[P]; }
type PartialByKeys<T, K extends keyof T = keyof T> = Duplicate<Partial<Pick<T, K>> & Omit<T, K>>
Solution by wzc520pyfm #31482
// your answers
type Merge<T> = {
[K in keyof T]: T[K]
}
type PartialByKeys<T, K = keyof T> = Merge<Partial<Pick<T,K & keyof T>> & Omit<T, K & keyof T>>;
Solution by kangaroona #31151
type IntersectionToObj<T> = {
[K in keyof T]: T[K]
}
type PartialByKeys<T, K extends keyof T = keyof T> = IntersectionToObj<Partial<Pick<T, K>> & Omit<T, K>>;
Solution by kai-phan #30925
type Unify<T> = {
[Key in keyof T]: T[Key]
}
type PartialByKeys<T, K extends keyof T = keyof T> = Unify<{
[Key in K]?: T[Key]
} & {
[Key in Exclude<keyof T, K>]: T[Key]
}>
Solution by maximallain #30867
type PartialByKeys<T, K extends keyof T = keyof T> = Omit<{
[Key in keyof T as Key extends K ? Key : never]?: T[Key]
} & {
[Key in Exclude<keyof T, K>]: T[Key]
}, never>
Solution by matallui #30805
// your answers
type PartialByKeys<T, K extends keyof T = keyof T> = Omit<{[Key in K]?: T[Key]} & Omit<T, K>, never>;
Surprisingly didn't find someone submitted this
Solution by orl0 #30744
type PartialByKeys<T, K extends keyof T = keyof T> = Omit<{
[U in keyof T as U extends K ? never : U]: T[U]
} & {
[U in keyof T as U extends K ? U : never]?: T[U]
}, never>
Solution by dreamluo-plus #30583
type Intersection<T> = {
[S in keyof T]: T[S]
}
type PartialByKeys<T, K extends keyof T = keyof T> = Intersection<{
[S in keyof T as S extends K ? S : never]?:T[S]
} &{
[S in keyof T as S extends K ? never: S] : T[S]
}>
Solution by Yoon-Hae-Min #30510
// 大部分解答不能捕获传入的key 在 T上不存在, 用下面的实现则可以
type MyExclude<T, U> = T extends U ? never : T
type MergeInterface<T> = {
[P in keyof T ]: T[P]
}
type PartialByKeys<T, K = never> = [K] extends [never] ? {
[P in keyof T ]?: T[P]
} : [MyExclude<K, keyof T>] extends [never] ? MergeInterface<{
[P in keyof T as P extends K ? never : P]: T[P]
} & {
[P in keyof T as P extends K ? P : never]?: T[P]
}> : never
Solution by xiahouwei #30380
type Assemble<T> = {
[K in keyof T]: T[K]
}
type Partial<T> = {
[P in keyof T]?: T[P]
}
type Extract<T, K> = {
[P in keyof T as P extends K ? P : never]?: T[P]
}
type Exclude<T, K> = {
[P in keyof T as P extends K ? never : P]: T[P]
}
type PartialByKeys<T, K = any> = Assemble<Partial<Extract<T, K>> & Exclude<T, K>>
Solution by tclxshunquan-wang #29802
type PartialByKeys<T, E extends keyof T = keyof T> = {
[k in E]?: T[k]
} & {
[k in keyof T as k extends E ? never: k]: T[k]
};
type User = PartialByKeys<{
name: string;
age: number;
address: string;
}, 'name'>;
const user: User = {
age: 1,
address: ''
}
Solution by sundial-dreams #29484
Solution:
type Merge<T, U> = Pick<T & U, keyof (T & U)>;
type PartialByKeys<T, K = keyof T> = Merge< Pick<Partial<T>, Extract<keyof T, K>>, Pick<T, Exclude<keyof T, K>> >
Solution by DmitriiBr #29354
// your answers
// your answers
type GenerateNewType<T> = {
[K in keyof T]: T[K];
};
type PartialByKeys<T, K extends keyof T = keyof T> = GenerateNewType<{
[U in keyof T as U extends K ? U : never]?: T[U];
} & {
[U in keyof T as U extends K ? never : U]: T[U];
}>
Solution by kerolossamir165 #29210
type PartialByKeys<O, K extends keyof O> = { [P in K]?: O[P] } & { [P in keyof O as P extends K ? never : P]: O[P] }
Solution by sunhk123456 #28954
/**
* Partial<Pick<T, K>> 取出需要Partial的key
* Omit<T, K> 排除需要Partial的key
* 此时两个类型是Intersection type,不符合Equal校验,利用Omit一个never key将两个类型合并一次
* 或者遍历一遍这个Intersection type
*/
type PartialByKeys<T, K extends keyof T = keyof T> =
Omit<
Partial<Pick<T, K>> & Omit<T, K>
, never
>
// type PartialByKeys<T, K extends keyof T = keyof T> = IntersectionToObj<
// Partial<Pick<T, K>> & Omit<T, K>
// >
// type IntersectionToObj<T> = {
// [K in keyof T]: T[K]
// }
Solution by rodrick278 #28933
type PartialByKeys<T, K extends keyof T = keyof T> = Omit<{
[Key in keyof T as Exclude<Key,K>]:T[Key]
}&{
[Key in K]?:T[Key]
},never>
Solution by DoubleWoodLin #28693