zl程序教程

您现在的位置是:首页 >  前端

当前栏目

[Typescript] 49. Medium - PartialByKeys

typescript Medium 49
2023-09-14 08:59:11 时间

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>>,
]