type OptionalKeys<T> = keyof {
[k in keyof T as T[k] extends Required<T>[k] ? never : k]: T[k]
}
Solution by wendao-liu #35170
type GetOptionalKeys<T extends object> = {
[K in keyof T as T[K] extends Required<T>[K] ? never : K]: T[K]
}
type OptionalKeys<T extends object> = keyof GetOptionalKeys<T>
Solution by ouzexi #34209
SO HARD
// your answers
type Convert<T extends object> = { [P in keyof T]-?: {} extends Pick<T,P> ? never : true }
type OptionalKeys<T extends object> = keyof {
[K in keyof Convert<T> as [Convert<T>[K]] extends [never] ? K : never]: Convert<T>[K]
}
Solution by kakasoo #32978
// your answers
type IsEqual<S,U> = (<T>() => T extends S ? 1 : 2) extends (<T>() => T extends U ? 1 : 2) ? true : false ;
type ExtractProperty<T extends object,K extends keyof T> = { [v in K]:T[v]} ;
type ResumeUndefined<T> = T extends object ? {
[key in keyof T]:T[key] extends never ? undefined :T[key];
}
:T;
type MyRequired<T> = ResumeUndefined<Required<T>>;
type OptionalKeys<T extends object> = keyof {
[key in keyof T as IsEqual<ExtractProperty<T,key>,ExtractProperty<MyRequired<T>,key>> extends false ? key : never]:true
}
Solution by sciencefunq #32915
type OptionalKeys<T> = {
[P in keyof T as T[P] extends Required<T>[P] ? never : P]: P;
} extends infer R
? Required<R>[keyof R]
: never;
Solution by vipulpathak113 #31811
type OptionalKeys
Solution by Kying-star #28983
type MyRequired<T> = {
[key in keyof T]-?: T[key]
}
type GetOptional<T> = {
[key in keyof T as T[key] extends MyRequired<T>[key] ? never : key]: key
}
// use MyRequired remove undefined
type OptionalKeys<T> = MyRequired<GetOptional<T>>[keyof MyRequired<GetOptional<T>>]
Solution by wuxin0011 #27560
type OptionalKeys<T> = keyof { [K in keyof T as { [K2 in K]?: T[K] } extends Pick<T, K> ? K : never]: T[K] }
Solution by smileboyi #27496
// 你的答案
type GetOptional<T> = {
[P in keyof T as T[P] extends Required<T>[P] ? never : P]: T[P]
}
type OptionalKeys<T> = keyof GetOptional<T>
Solution by kiki-zjq #26039
// your answers
type OptionalKeys<T extends object> = keyof {
[K in keyof T as T[K] extends Required<T>[K] ? never : K]: T[K]
}
Solution by studymachiney #24959
type GetOptional<T> = { [P in keyof T as T[P] extends Required<T>[P] ? never : P]: T[P] };
type OptionalKeys<T> = keyof GetOptional<T>;
Solution by E-uler #24672
type OptionalKeys<T> =keyof {
[Key in keyof T as T[Key] extends Required<T>[Key] ? never : Key]:Key
}
Solution by NeylonR #24417
type OptionalKeys<T> = keyof {[Key in keyof T as {} extends Pick<T, Key> ? Key: never]: T[Key]}
Solution by sabercc #23864
type GetOptional<T> = {
[K in keyof T as {} extends Pick<T, K> ? K : never]: T[K]
}
type OptionalKeys<T> = keyof GetOptional<T>
Solution by snakeUni #23412
type GetOptional<T> = {
[Key in keyof T as T[Key] extends Required<T>[Key] ? never : Key]: T[Key]
}
type OptionalKeys<T> = keyof GetOptional<T>
Solution by drylint #22197
type GetOptional<T extends object> = {[K in keyof T as T[K] extends Required<T>[K] ? never : K]: T[K]}
type OptionalKeys<T extends object> = keyof GetOptional<T>
Solution by kfess #21922
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 OptionalKeys<T extends object> = {} extends T ? never : ObjectKeys<Omit<T,keyof GetRequired<T>>>;
Solution by Karamuto #21887
type OptionalKeys<T extends object, K = keyof T> = K extends keyof T
? T extends Required<Pick<T, K>>
? never
: K
: never
Solution by jgjgill #21715
type OptionalKeys<T extends object, K = keyof T> = K extends keyof T
? Omit<T, K> & { K?: T[K] } extends T
? K
: never
: never
Solution by milletlovemouse #21551
type UnionToIntersection<U> = (
U extends U ? (x: U) => unknown : never
) extends (x: infer R) => unknown
? R
: never;
type GetOptional<T, S extends keyof T = keyof T> = Partial<
UnionToIntersection<
S extends S
? Equal<
Pick<T, S>,
Partial<{
[P in S]: T[S];
}>
> extends true
? Pick<T, S>
: never
: never
>
>;
type OptionalKeys<T> = keyof GetOptional<T>
Solution by so11y #21022
// 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 OptionalKeys<T> = keyof {
[K in keyof T as GetRequiredKeys<T,K> extends GetNotRequriredKeys<T,K> ? never : K] : K
}
Solution by YqxLzx #20454
// your answers
type RequiredKeys1<T> = C<GetOptional<T>>[keyof C<GetOptional<T>>];
Solution by fengjinlong #20168
type IsOptionalKey<T, K extends keyof T> =
{[P in K]+?: T[P]} extends {[P in K]: T[P]}
? true
: false
type GetOptional<T> = {
[P in keyof T as IsOptionalKey<T, P> extends true ? P : never]: T[P]
}
type OptionalKeys<T> = keyof GetOptional<T>
see: https://github.com/type-challenges/type-challenges/issues/20149
Solution by zhaoyao91 #20152
type GetOptionalObj<T extends Record<PropertyKey, unknown>> = {
[key in keyof T as { [p in key]: T[p] } extends Required<{ [p in key]: T[p] }> ? never : key]: T[key];
};
type OptionalKeys<T extends Record<PropertyKey, unknown>> = keyof GetOptionalObj<T>;
Solution by CaoXueLiang #18959
type OptionalKeys<T> = keyof {
[K in keyof T as T[K] extends Required<T>[K] ? never : K]: T[K]
}
Solution by milletlovemouse #18364
// your answers
type GetOptional<T> = {
[K in keyof T as {[k in K]?: any} extends {[k in K]: T[K]} ? K : never]: T[K];
};
type OptionalKeys<T> = keyof GetOptional<T>
Solution by seho-dev #17225
type GetOptional<T> = {
[K in keyof T as {[key in K]?: any} extends {[key in K]: T[key]} ? K : never]: T[K]
}
type OptionalKeys<T> = keyof GetOptional<T>
Solution by abdurahmanus #17115
// your answers
type SelfRequired<T> = {
[K in keyof T]-?: T[K];
};
type GetOptional<T> = {
[K in keyof T as (
{
[P in K]: T[P];
} extends SelfRequired<{
[P in K]: T[P];
}> ? never : K
)]: T[K];
};
type OptionalKeys<T> = keyof GetOptional<T>;
Solution by jiaaoMario #17051
// your answers
type OptionalKeys<T> = keyof {
[P in keyof T as Omit<T, P> extends T ? P : never]: T[P]
}
Solution by humandetail #16388
// your answers
type OptionalKeys<T> = keyof {
[p in keyof T as Omit<T, p> extends T ? p : never]: T[p]
}
Solution by Stan-BK #16278