Implement a generic PartialByKeys<T, K>
which takes two type argument T
and K
.
K
specify the set of properties of T
that should set to be optional. When K
is not provided, it should make all properties optional just like the normal Partial<T>
.
For example
interface User {
name: string
age: number
address: string
}
type UserPartialName = PartialByKeys<User, 'name'> // { name?:string; age:number; address:string }
/* _____________ Your Code Here _____________ */
type MergeObject<T> = { [P in keyof T]: T[P] };
type PartialByKeys<T, K extends keyof T = keyof T> = MergeObject<
Omit<T, K> & {
[P in keyof T as P extends K ? P : never]?: T[P];
}>;
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
interface User {
name: string
age: number
address: string
}
interface UserPartialName {
name?: string
age: number
address: string
}
interface UserPartialNameAndAge {
name?: string
age?: number
address: string
}
type cases = [
Expect<Equal<PartialByKeys<User, 'name'>, UserPartialName>>,
Expect<Equal<PartialByKeys<User, 'name' | 'age'>, UserPartialNameAndAge>>,
Expect<Equal<PartialByKeys<User>, Partial<User>>>,
// @ts-expect-error
Expect<Equal<PartialByKeys<User, 'name' | 'unknown'>, UserPartialName>>,
]
标签:Typescript,string,PartialByKeys,number,Medium,address,type,name From: https://www.cnblogs.com/Answer1215/p/16792701.html