[Typescript] 95. Hard - Required Keys
Implement the advanced util type RequiredKeys<T>
, which picks all the required keys into a union.
For example
type Result = RequiredKeys<{ foo: number; bar?: string }>;
// expected to be “foo”
/* _____________ Your Code Here _____________ */
type RequiredKeys<T extends Record<PropertyKey, any>> = keyof {
[Key in keyof T as T[Key] extends Required<T>[Key] ? Key: never]: any
}
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<RequiredKeys<{ a: number; b?: string }>, 'a'>>,
Expect<Equal<RequiredKeys<{ a: undefined; b?: undefined }>, 'a'>>,
Expect<Equal<RequiredKeys<{ a: undefined; b?: undefined; c: string; d: null }>, 'a' | 'c' | 'd'>>,
Expect<Equal<RequiredKeys<{}>, never>>,
]