Implement the util type OptionalUndefined<T, Props>
that turns all the properties of T
that can be undefined
, into optional properties. In addition, a second -optional- generic Props
can be passed to restrict the properties that can be altered.
OptionalUndefined<{ value: string | undefined, description: string }>
// { value?: string | undefined; description: string }
OptionalUndefined<{ value: string | undefined, description: string | undefined, author: string | undefined }, 'description' | 'author'>
// { value: string | undefined; description?: string | undefined, author?: string | undefined }
Solution:
/* _____________ Your Code Here _____________ */
type Merge<T> = {
[K in keyof T]:T[K]
}
type OptionalUndefined<
T,
Props extends keyof T = keyof T,
OptionsProps extends keyof T = Props extends keyof T
? undefined extends T[Props]
? Props
:never
:never
> =
Merge<{
[K in OptionsProps]?: T[K]
} & {
[K in Exclude<keyof T, OptionsProps>]: T[K]
}>
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
import { ExpectFalse, NotEqual } from '@type-challenges/utils'
type cases = [
Expect<Equal<OptionalUndefined<{ value: string | undefined }, 'value'>, { value?: string | undefined }>>,
Expect<Equal<OptionalUndefined<{ value: string; desc: string }, 'value'>, { value: string; desc: string }>>,
Expect<Equal<OptionalUndefined<{ value: string | undefined; desc: string }, 'value'>, { value?: string; desc: string }>>,
Expect<Equal<OptionalUndefined<{ value: string | undefined; desc: string | undefined }, 'value'>, { value?: string | undefined; desc: string | undefined }>>,
Expect<Equal<OptionalUndefined<{ value: string | undefined; desc: string }, 'value' | 'desc'>, { value?: string; desc: string }>>,
Expect<Equal<OptionalUndefined<{ value: string | undefined; desc: string | undefined }>, { value?: string; desc?: string }>>,
Expect<Equal<OptionalUndefined<{ value?: string }, 'value'>, { value?: string }>>,
Expect<Equal<OptionalUndefined<{ value?: string }>, { value?: string }>>,
]
标签:150,Typescript,string,OptionalUndefined,value,Expect,type,undefined From: https://www.cnblogs.com/Answer1215/p/17563989.html