Implement the advanced util type OptionalKeys<T>
, which picks all the optional keys into a union.
/* _____________ Your Code Here _____________ */
type OptionalKeys<T extends Record<PropertyKey, any>> = keyof {
[Key in keyof T as T[Key] extends Required<T>[Key] ? never: Key]: any
}
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<OptionalKeys<{ a: number; b?: string }>, 'b'>>,
Expect<Equal<OptionalKeys<{ a: undefined; b?: undefined }>, 'b'>>,
Expect<Equal<OptionalKeys<{ a: undefined; b?: undefined; c?: string; d?: null }>, 'b' | 'c' | 'd'>>,
Expect<Equal<OptionalKeys<{}>, never>>,
]
标签:Typescript,_____________,Keys,Hard,keyof,Expect,Key,type From: https://www.cnblogs.com/Answer1215/p/16879315.html