type RequiredKeys<T> = keyof {
[k in keyof T as T[k] extends Required<T>[k] ? k : never]: T[k]
}
Solution by wendao-liu #35169
type RequiredKeys<T> = keyof {
[Key in keyof T as T[Key] extends Required<T>[Key] ? Key : never]: T[Key]
}
Solution by Hencky #34973
type IsKeyOptional<T extends Record<string, any>, K extends keyof T> = {
[P in K]?: T[P]
} extends Pick<T, K> ? true : false
type RequiredKeys<T extends Record<string, any>, K extends keyof T = keyof T> = {
[P in K]-?: IsKeyOptional<T, P> extends true ? never : P
}[K]
Solution by ktim816 #34447
type GetRequiredKeys<T extends object> = {
[K in keyof T as T[K] extends Required<T>[K] ? K : never]: T[K]
}
type RequiredKeys<T extends object> = keyof GetRequiredKeys<T>
Solution by ouzexi #34208
type RequiredKeys<T> = keyof {
[P in keyof T as T[P] extends Required<T>[P] ? P : never]: T[P];
};
Solution by Vampirelee #32604
type RequiredKeys<T> = {
[P in keyof T as T[P] extends Required<T>[P] ? P : never]: P;
} extends infer R
? R[keyof R]
: never;
Solution by vipulpathak113 #31796
type RequiredKeys<T> = keyof {
[P in keyof T as T[P] extends Required<T>[P]? P : never ]: ""
}
Solution by ethansnow2012 #31099
type RequiredKeys<T> = keyof {
[K in keyof T as Pick<T, K> extends Required<Pick<T, K>> ? K : never]: T[K]
}
Solution by dreamluo-plus #30720
type RequiredKeys<T> = keyof {
[K in keyof T as T[K] extends Required<T>[K] ? K : never]: T[K]
}
Solution by Forthtilliath #29977
type MyRequried<T> = {
[key in keyof T]-?: T[key]
}
type GetRequired<T> = {
[key in keyof T as T[key] extends MyRequried<T>[key] ? key : never]: key
}
type RequiredKeys<T> = GetRequired<T>[keyof GetRequired<T>]
Solution by wuxin0011 #27556
type RequiredKeys<T, K = keyof T> = K extends keyof T ? T extends Required<Pick<T, K>> ? K : never : never
Solution by smileboyi #27413
// 你的答案
type GetRequired<T> = {
[key in keyof T as T[key] extends Required<T>[key] ? key : never]: T[key]
}
type RequiredKeys<T extends Object> = keyof GetRequired<T>
Solution by kiki-zjq #25985
// your answers
type RequiredKeys<T extends object> = keyof {
[K in keyof T as T[K] extends Required<T>[K] ? K : never]: T[K]
}
Solution by studymachiney #24958
type GetRequired<T> = { [P in keyof T as T[P] extends Required<T>[P] ? P : never]: T[P] };
type RequiredKeys<T> = keyof GetRequired<T>;
Solution by E-uler #24671
type RequiredKeys<T> =keyof {
[Key in keyof T as T[Key] extends Required<T>[Key] ? Key : never]:Key
}
Solution by NeylonR #24416
// 你的答案
type RequiredKeys<T> = keyof {
[K in keyof T as T[K] extends Required<T>[K] ? K:never]:T[K]
}
Solution by walker-hzx #24298
type RequiredKeys<T> = keyof{[Key in keyof T as {} extends Pick<T, Key> ? never: Key]: T[Key]}
Solution by sabercc #23862
type GetRequired<T> = {
[K in keyof T as {} extends Pick<T, K> ? never : K]: T[K]
}
type RequiredKeys<T> = keyof GetRequired<T>
Solution by snakeUni #23411
// 你的答案
type GetRequired<T> = {
[K in keyof T as {} extends Pick<T, K> ? never : K]: T[K];
};
type RequiredKeys<T> = keyof GetRequired<T>;
Solution by jxhhdx #22819
type GetRequired<T> = {
[Key in keyof T as T[Key] extends Required<T>[Key] ? Key : never]: T[Key]
}
type RequiredKeys<T> = keyof GetRequired<T>
Solution by drylint #22196
type GetRequired<T extends object> = {[K in keyof T as T[K] extends Required<T>[K] ? K : never]: T[K]}
type RequiredKeys<T extends object> = [...Array<keyof GetRequired<T>>][number]
Solution by kfess #21920
type GetRequired<T> = {
[key in keyof T as T[key] extends Required<T>[key] ? key : never] : T[key]
}
type ObjectKeys<T, K = keyof T> = K extends K ? K : never;
type RequiredKeys<T extends object> = {} extends T ? never : ObjectKeys<GetRequired<T>>;
Solution by Karamuto #21885
type IsRequired<T, P extends keyof T = keyof T> = { [k in P]: T[k] } extends { [k in P]-?: T[k] } ? true : false;
type OnlyRequired<T> = { [k in keyof T as IsRequired<T, k> extends true ? k : never]: T[k] };
type RequiredKeys<T> = keyof OnlyRequired<T>;
Solution by ivbrajkovic #21629
type UnionToIntersection<U> = (
U extends U ? (x: U) => unknown : never
) extends (x: infer R) => unknown
? R
: never;
type GetRequired<T, S extends keyof T = keyof T> = Required<
UnionToIntersection<
S extends S
? Equal<
Pick<T, S>,
Required<{
[P in S]: T[S];
}>
> extends true
? Pick<T, S>
: never
: never
>
>;
type RequiredKeys<T> = keyof GetRequired<T>
Solution by so11y #21021
// your answers
type GetRequiredKeys<T,P extends keyof T> = {
[K in keyof T] : T[K]
}[P]
type GetNotRequriredKeys<T,P extends keyof T> = {
[K in keyof T]-? : T[K]
}[P]
type RequiredKeys<T> = keyof {
[K in keyof T as GetRequiredKeys<T,K> extends GetNotRequriredKeys<T,K> ? K : never] : K
}
Solution by YqxLzx #20453
// your answers
/**
* 1 必须属性
* 2 val 变 key
* 3 T[keyof T]
*/
// 2
type C<T> = {
[Key in keyof T]: Key;
};
type RequiredKeys<T> = C<GetRequired<T>>[keyof C<GetRequired<T>>];
Solution by fengjinlong #20167
type IsRequiredKey<T, K extends keyof T> =
{[P in K]: T[P]} extends {[P in K]-?: T[P]}
? true
: false
type GetRequired<T> = {
[P in keyof T as IsRequiredKey<T, P> extends true ? P : never]: T[P]
}
type RequiredKeys<T> = keyof GetRequired<T>
see: https://github.com/type-challenges/type-challenges/issues/20148
Solution by zhaoyao91 #20151
type GetRequireObj<T extends Record<PropertyKey, unknown>> = {
[key in keyof T as { [p in key]: T[p] } extends Required<{ [p in key]: T[p] }> ? key : never]: T[key];
};
type RequiredKeys<T extends Record<PropertyKey, unknown>> = keyof GetRequireObj<T>;
Solution by CaoXueLiang #18957
type RequiredKeys<T> = keyof {
[K in keyof T as
Omit<T, Exclude<keyof T, K>> extends Required<Omit<T, Exclude<keyof T, K>>>
? K
: never]: T[K]
}
Solution by milletlovemouse #18362
type RequiredKeys<T> = keyof { [R in keyof T as T[R] extends Required<T>[R] ? R: never]: never }
Solution by xjq7 #17279