type IsRequiredKey<T, K extends keyof T> = Pick<T, K> extends Required<Pick<T, K>> ? true : false
Solution by ouzexi #34302
type IsRequiredKey<T, K extends keyof T, P = Pick<T, K>> = Equal<P,Required<P>>
Solution by Vampirelee #32636
type BoolCheck<T> = boolean extends T ? false : T
type IsRequiredKey<T, K extends keyof T> = BoolCheck<K extends keyof {
[Key in K as {[P in Key]?:T[P]} extends Pick<T,Key> ? never : Key]:T[Key]
} ? true : false>
Solution by idebbarh #30952
type IsRequiredKey<T, K extends keyof T> = [K] extends [keyof {[R in keyof T as Omit<T, R> extends T ? never : R]: never}] ? true : false
This, however, returns true
when the K
parameter is never
. I wasn't sure if I should deal with this, because there's nothing about it in cases
in the playground.
Solution by octet-stream #30289
// your answers
type IsRequiredKey<T, K extends keyof T> = T extends Required<{[Key in K]: T[K]}> ? true : false
Solution by 437204933 #29706
type IsRequiredKey<T extends object, K extends keyof T> = T[K] extends Required<T>[K] ? true : false
Solution by simone-paglino #29648
type IsRequiredKey<T, K extends keyof T> = T[K] extends Required<T>[K] ? true : false; // string | undefined extends string ==> false
// other way
// type IsRequiredKey<T, K extends keyof T> = Pick<T, K> extends Required<Pick<T, K>> ? true : false;
Solution by E-uler #24974
// your answers
type IsRequiredKey<T, K extends keyof T> = Pick<T, K> extends {
[Key in K]-?: T[Key]
} ? true : false;
Solution by jxhhdx #24225
// your answers
type IsRequiredKey<T, K extends keyof T> = Pick<T, K> extends {
[Key in K]-?: T[K]
} ? true : false
Solution by snakeUni #23971
type IsRequiredKey<T, K> = ((args:{[P in keyof T]-? : T extends Record<P, T[P]> ? P : never}[keyof T]) => void) extends ((args:K)=>any)
? true
: false
Solution by TKBnice #23361
type IsRequiredKey<T, K extends keyof T> =
{ [Key in K]: T[K] } extends { [Key in K]-?: T[K] }
? true
: false
Failing cases:
// Failing: Expect<Equal<IsRequiredKey<{ a: undefined; b: string }, 'a' | 'b'>, true>>
type IsRequiredKey<T, K extends keyof T> =
T[K] | undefined extends T[K] ? false : true
// Failing: Expect<Equal<IsRequiredKey<{ a: undefined; b?: string }, 'a' | 'b'>, false>>
type IsRequiredKey<T, K extends keyof T> =
T[K] extends Required<T>[K] ? true : false
Solution by drylint #22749
// T[K] returns an union which includes "undefined" if there was any non required field included in the union of keys
type IsRequiredKey<T, K extends keyof T> = T[K] | undefined extends T[K] ? false : true;
Solution by Karamuto #22203
type IsRequiredKey<T, K extends keyof T> =
{[P in K]: T[P]} extends {[P in K]-?: T[P]}
? true
: false
Insteresting, I have solved this challenge in previous chanllenges.
Related
Solution by zhaoyao91 #21571
// your answers
type IsRequiredKey<T, K extends keyof T> = T[K] | undefined extends T[K] ? false : true
Solution by YqxLzx #21532
type IsRequiredKey<T, K extends keyof T> = undefined extends T[K] ? false : true
Solution by liuseen-l #21307
// your answers
type IsRequiredKey<T, K extends keyof T> = T extends Record<K, T[K]> ? true : false
Solution by fengjinlong #20298
type IsRequiredKey<T, K extends keyof T> = undefined extends T[K] ? false : true
Solution by lvjiaxuan #20190
type GetRequiredKey<T> = keyof {
[key in keyof T as { [p in key]: T[p] } extends Required<{ [p in key]: T[p] }> ? key : never]: T[key];
};
// 注ζιθ¦η¨ζΉζ¬ε·[]ζ¬θ΅·ζ₯
type IsRequiredKey<T, K extends keyof T> = [K] extends [GetRequiredKey<T>] ? true : false;
Solution by CaoXueLiang #19477
type IsRequiredKey<T, K extends keyof T, R = Pick<T, K>> = R extends Required<R> ? true : false;
// Missing case
// Expect<Equal<IsRequiredKey<{ a: number; b: string }, 'a' | 'b'>, true>>
Solution by BulatDashiev #16947
type IsRequiredKey<T extends {}, R> = (
Solution by my5201314zwl #16703
// your answers
type RequiredKeys<T> = keyof {
[P in keyof T as Omit<T, P> extends T ? never : P]: T[P]
}
type IsRequiredKey<T, K extends keyof T> = Equal<RequiredKeys<T>, K>
Solution by humandetail #16487
// your answers
type IsRequiredKey<
T extends object,
K extends keyof T,
O extends object = {[J in K] : T[J]} > =
O extends Required<O>
? true
: false ;
Solution by justBadProgrammer #15990
type IsRequiredKey<T, K extends keyof T> = Equal<{[P in K]?: T[P]}, {[P in K]: T[P]}> extends true ? false : Equal<{[P in K]-?: T[P]}, {[P in K]: T[P]}>;
Solution by sromic #15417
// your answers
type IsRequiredKey<T, K extends keyof T> = T extends { [k in K] -?: T[k] } ? true : false
Solution by userakf #15360
type IsRequiredKey<T, K extends keyof T> = (
K extends keyof T ? ({} extends Pick<T, K> ? 1 : 0) : never
) extends 0
? true
: false
Solution by yukinotech #13664
// your answers
type IsRequiredKey<T, K extends keyof T> = {
[Key in K]: T[K]
} extends Required<{
[Key in K]: T[K]
}> ? true : false
Solution by liuxing95 #13121
// your answers
type IsRequiredKey<T, K extends keyof T> = undefined extends T[K]
? false
: true
Solution by GumplinGo #13033
type IsRequiredKey<
TObject,
TKey extends keyof TObject,
TRequiredObject extends TObject = Required<TObject>
> = TObject[TKey] extends TRequiredObject[TKey] ? true : false
Solution by michaltarasiuk #12407
// your answers
type IsRequiredKey<T, K extends keyof T> = T[K] extends Required<T>[K]
? true
: false;
Solution by SeptEarlyMorning #12206
type IsRequiredKey<T, K extends keyof T> =
MyEqual<{ [P in K]: T[P] }, { [P in K]-?: T[P] }>
type MyEqual<A, B> = (<T>() => T extends A ? 1 : 0) extends (<T>() => T extends B ? 1 : 0) ? true : false
Solution by teamchong #11782