00057-hard-get-required

Back

type GetRequired<T> = {
  [k in keyof T as T[k] extends Required<T>[k] ? k : never]: T[k]
}

Solution by wendao-liu #35167

type GetRequired<T> = {[K in keyof T as Omit<T, K> extends T ? never : K]: T[K]}

Solution by cipak #34641

type GetRequired<T> = {
  [K in keyof T as T extends Record<K, T[K]> ? K : never]: T[K]
};

Solution by skeleton1231 #34344

type GetRequired<T extends object> = {
  [K in keyof T as T[K] extends Required<T>[K] ? K : never]: T[K]
}

Solution by ouzexi #34201

type GetRequired = { [K in keyof T as {} extends Pick<T, K> ? never : K]: T[K] }

Solution by SiteBuddha #33928

思路:

  1. 利用 Required 将类型T中所有的属性去掉?
  2. T中的每个字段与去掉可选?的字段判断是否一致,如果一致,则保留,不一致就去掉。
  3. Equal 为例子中的工具函数,可以直接使用,其原理: type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false
type GetRequired<T, M = Required<T>> = {
  [P in keyof (T|M) as Equal<T[P], M[P]> extends true ? P : never]: T[P];
};

Solution by Vampirelee #32602

// your answers
type OwnRequired<T> = { [P in keyof T]-?: T[P] }
type GetRequired<T> = {
  [K in keyof T as T[K] extends OwnRequired<T>[K] ? K : never]: T[K]
}

Solution by laqudee #32360

type UnionToIntersection<U> = (
  U extends unknown ? (arg: U) => 0 : never
) extends (arg: infer I) => 0
  ? I
  : never;

type LastInUnion<U> = UnionToIntersection<
  U extends unknown ? (x: U) => 0 : never
> extends (x: infer L) => 0
  ? L extends U
    ? L
    : never
  : never;

type GetRequired<
  T,
  L extends keyof T = keyof T,
  R extends keyof T = never,
  Last extends keyof T = LastInUnion<L>
> = [L] extends [never]
  ? Pick<T, R>
  : Pick<T, Last> extends { [P in keyof T & Last]: T[P] }
  ? GetRequired<T, Exclude<L, Last>, R | Last>
  : GetRequired<T, Exclude<L, Last>, R>;

Solution by vangie #32237

type GetRequired<T> = { [P in keyof T as T[P] extends Required<T>[P] ? P : never]: T[P] };

Solution by vipulpathak113 #31319

type GetRequired<T> = {
  [K in keyof T as Pick<T, K> extends Required<Pick<T, K>> ? K : never]: T[K]
}

Solution by dreamluo-plus #30718

type GetRequired<T> = {
    [Key in keyof T as Omit<T,Key> extends T ? never : Key] : T[Key];
}

Solution by idebbarh #30338

// your answers
type GetRequired<T> = {
  [Key  in keyof T as T[Key] extends Required<T>[Key] ? Key  : never ]: T[Key]
}

Solution by kerolossamir165 #28446

type MyRequired<T> = {
  [P in keyof T]-?: T[P]
}

type GetRequiredKeys<T, U = MyRequired<T>, S extends keyof U = keyof U> = S extends keyof T
  ? T[S] extends U[S]
    ? S
    : never
  : never

type MyPick<T, S> = {
  [P in keyof T as P extends S ? P : never]: T[P]
}

type GetRequired<T> = MyPick<T, GetRequiredKeys<T>>

Solution by jazelly #27712

A bit easier to read

type GetRequiredKeys<A, B> = {
  [K in keyof A & keyof B]: A[K] extends B[K] ? K : never
}

type GetRequired<T> = Pick<T, GetRequiredKeys<T, Required<T>>[keyof T]>

Solution by furkandmrblk #27665

// 使得所有key变成必选类型
type MyRequried<T> = {
  [k in keyof T]-?: T[k]
}


type GetRequired<T> = {
  [key in keyof T as T[key] extends MyRequried<T>[key] ? key : never]: T[key]
}

Solution by wuxin0011 #27555

type GetRequired<T> = { [K in keyof T as T[K] extends Required<T>[K] ? K : never]: T[K] }

Solution by smileboyi #27331

// 你的答案
type GetRequired<T> = {
  [key in keyof T as T[key] extends Required<T>[key] ? key : never]: T[key]
}

Solution by kiki-zjq #25983

// your answers
type GetRequired<T extends object> = {
  [P in keyof T as T[P] extends Required<T>[P] ? P : never]: T[P]
}

Solution by studymachiney #24955

type GetRequired<T extends object> = { [P in keyof T as Record<P, T[P]> extends Required<{ [_X in P]: T[P] }> ? P : never]: T[P] };

// old way
//利用本身undefined -?后变为never的特性,剔除{ x?: undefined }字段
// type GetRequiredFromUndefined<T> = { [P in keyof Required<T> as[Required<T>[P]] extends [never] ? never : P]: Required<T>[P] };

// type GetRequired<T, _Var = GetRequiredFromUndefined<T>> = { [P in keyof _Var as P extends keyof T ? (T[P] extends _Var[P]/*判断非 { x?: type | undefined } extends { x: type } */ ? P : never) : P]: _Var[P] };

Solution by E-uler #24669

type GetRequired<T> = {
  [Key in keyof T as T[Key] extends Required<T>[Key] ? Key : never]: T[Key]
}

Solution by NeylonR #24414

// 你的答案
type GetRequired<T> = {
  [K in keyof T as T[K] extends Required<T>[K] ?K:never]:T[K]
}

Solution by walker-hzx #24296

// your answers
type GetRequired<T> = {
  [P in keyof T as Omit<T,P> extends T ? never: P]: T[P]
}

Solution by edisonLzy #23917

type GetRequired<T> = {[Key in keyof T as  {} extends Pick<T, Key> ? never: Key]: T[Key]}

Solution by sabercc #23855

type GetRequired<T> = {
  [K in keyof T as {} extends Pick<T, K> ? never : K]: T[K]
}

Solution by snakeUni #23408

// 你的答案
type GetRequired<T> = {
  [K in keyof T as {} extends Pick<T, K> ? never : K]: T[K]
}

Solution by jxhhdx #22815

type GetRequired<T> = {
  [Key in keyof T as T[Key] extends Required<T>[Key] ? Key : never]: T[Key]
}

Solution by drylint #22190

// your answers
type SingleKey<T, Key extends keyof T> = T & Key extends string ? {
  [key in keyof T as key extends Key ? Key : never]: T[Key]
}: never

type GetRequired<T> = {
  [Key in keyof T as SingleKey<T, Key> extends T ? Key : never]: T[Key]
}

Solution by 437204933 #22043

type GetRequired<T> = {[K in keyof T as T[K] extends Required<T>[K] ? K : never]: T[K]}

Solution by kfess #21917

type GetRequired<T> = {
  [key in keyof T as T[key] extends Required<T>[key] ? key : never] : T[key]
}

Solution by Karamuto #21882

// 你的答案
type GetRequired<T>
    = { [K in keyof T as T[K] extends { [K in keyof T]-?: T[K] }[K] ? K : never]: T[K] }

Solution by goddnsgit #21843