02759-medium-requiredbykeys

Back

type Componse<T> = {
  [k in keyof T]: T[k]
}
type RequiredByKeys<T, K extends keyof T = keyof T> = Componse<Omit<T, K> & {
  [k in K]-?: T[k]
}>

Solution by wendao-liu #35052

type RequiredByKeys<T, K extends PropertyKey = keyof T> =  Omit<T & Required<Pick<T, K & keyof T>>, never>;

Solution by devshinthant #34931

type ExtractType<T, K extends keyof T = never> = [K] extends [never]
  ? { [P in keyof T]-?: T[P] }
  : { [P in K]-?: T[P] } & { [P in keyof T as P extends K ? never : P]?: T[P] }

type RequiredByKeys<T, K extends keyof T = never> = {
  [P in keyof ExtractType<T, K>]: ExtractType<T, K>[P]
}

Solution by Keith-Web3 #34781

// your answers
type RequiredByKeys<T, K extends keyof T = keyof T> = Omit<Required<Pick<T, K>> & Pick<T, Exclude<keyof T, K>>, never>

Solution by AndreGeng #34762

// your answers

type Intersection<T> = { 
  [K in keyof T]: T[K]
}

type RequiredByKeys<T, K extends keyof T = keyof T> =  Intersection<{
  [X in Exclude<keyof T, K>]+?: T[X]
} & {
  [X in K]-?: T[X]
}>

Solution by gobielJonathan #34528

type Obj<T extends object> = {
  [P in keyof T]: T[P]
}

type RequiredByKeys<T, K extends keyof T = never> = Obj<[K] extends [never] ? Required<T> : {
  [P in keyof T as P extends K ? P : never]-?: T[P]
} & Omit<T, K>>

Solution by ouzexi #34050

最简单的写法(开心)

// 你的答案
type RequiredByKeys<T, K extends keyof T = keyof T> =
Required<Pick<T,K>> & Omit<T,K> extends infer G 
? Pick<G,keyof G>:never

Solution by xdrlmm #33949

type RequiredByKeys<T, Key extends keyof T = keyof T> = Omit<T, Key> & Required<Pick<T, Key>>

Solution by ZhulinskiiDanil #32730

type RequiredByKeys<T, K extends keyof T = keyof T> = Omit<Omit<T, K> & { [P in keyof T as P extends K ? P : never]-?: T[P] }, never>

Solution by dev-hobin #32499

type RequiredByKeys<T, K extends keyof T = keyof T> = Omit<Required<Pick<T, K>> & Omit<T, K>, never>;

Solution by vangie #32173

type RequiredByKeys<T, K extends keyof T> = {
  [P in keyof T as P extends K ? never : P]: T[P];
} & {
  [P in keyof T as P extends K ? P : never]-?: T[P];
} extends infer I
  ? { [P in keyof I]: I[P] }
  : never;

Solution by vipulpathak113 #31743

// your answers
type Compute<T> = { [K in keyof T]: T[K] };
type RequiredByKeys<T, K extends keyof T = keyof T> = Compute<Required<Pick<T, K>> & Omit<T, K>>;

version 2023-12-12 v1.15.0

Solution by Qzhor #31614

type RequiredByKeys<T extends {}, K extends keyof T = keyof T> = Omit<{[Key in K]-?: T[Key]} & Omit<T, K>, never>

Solution by korkota #31123

// your answers
type Merge<T> = {
  [K in keyof T]: T[K]
}
type RequiredByKeys<T, K = keyof T> = Merge<T & Required<Pick<T, K & keyof T>>>

Solution by kangaroona #31077

type Intersection<T> = {
  [K in keyof T]: T[K]
}

type RequiredByKeys<T, K extends keyof T = keyof T> = Intersection<Required<Pick<T, K>> & Omit<T, K>>;```

Solution by kai-phan #30974

type Unify<T> = { [Key in keyof T]: T[Key] }
type RequiredByKeys<T, K extends keyof T = keyof T> = Unify<{
  [Key in keyof T as Key extends K ? Key : never]-?: T[Key]
} &{
  [Key in keyof T as Key extends K ? never : Key]: T[Key]
}>

Solution by maximallain #30868

type RequiredByKeys<T, K extends keyof T = keyof T> = Omit<Omit<T, K> & {
  [Key in K]-?: T[Key]
}, never>

Solution by matallui #30806

Take a page from type PartialByKeys

type IntersectionToObj<T> = {
	[K in keyof T]: T[K]
}

type RequiredByKeys<T, K = keyof T> = IntersectionToObj<{
	[P in keyof T as P extends K ? P : never]-?: T[P]
} & {
	[P in Exclude<keyof T, K>]?: T[P]
}>```

Solution by neMew5 #30801

type Keys<T> = {
  [P in keyof T]: T[P]
}
type RequiredByKeys<T, K = keyof T> = Keys<{
  [P in keyof T as P extends K ? P : never]-?: T[P]
} & { [P in Exclude<keyof T, K>]?: T[P]; }>

Solution by qiqi1112 #30651

type Normalize = { [K in keyof T]: T[K] }; type RequiredByKeys<T, K extends keyof T = never> = Normalize<Required<Pick<T, K>> & Pick<T, Exclude<keyof T, K>>>;

Solution by wouldRewind #30645

type InterSection<T>={
  [S in keyof T]: T[S]
}

type RequiredByKeys<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 #30511

// your answers

type GenerateNewType<T> = {
  [K in keyof T]: T[K];
};
type RequiredByKeys<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 #29732

// your answers
type RequiredByKeys<T, K extends keyof T = keyof T> = Omit<Omit<T, K> & Required<Pick<T, K>>, never>

Solution by nomiszzz #29664

type RequiredByKeys<T extends object, E extends keyof T = keyof T> = {
    [k in E]-?: T[k]
} & {
    [k in keyof T as k extends E ? never: k]?: T[k]
};

type User1 = RequiredByKeys<{
    name?: string;
    age?: number
}>;


Solution by sundial-dreams #29485

type RequiredByKeys<T, K extends keyof T = keyof T> = Omit<T & Required<Pick<T, K>>,never>

Solution by dmytro-shumak #29434

type RequiredByKeys<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 #28694

type RequiredByKeys<T, K extends keyof T = keyof T> = 
  Omit<Omit<T, K> & Required<Pick<T,K>>, never>

Solution by jjswifty #27627

// your answers
type Flatten<T> = {
  [K in keyof T]: T[K];
};

type RequiredByKeys<T, K extends keyof T = keyof T> = Flatten<
  Omit<T, K> & { [k in K]-?: T[k] }
>;

Solution by daiki-skm #27491

type RequiredByKeys<T, K extends keyof T> = Omit< T & Required<Pick<T, K>>,  never>

Solution by vuongManabie #27195

type Merge<F extends Record<any, any>, S extends Record<any, any>> = {
  [K in keyof (F & S)]: K extends keyof S ? S[K] : F[K]
}
type RequiredByKeys<T extends Record<any, any>, K extends keyof T = never> =
  [K] extends [never] ? Required<T> : Merge<Required<Pick<T, K>>, Omit<T, K>>

Solution by smileboyi #26982