OptionalUndefined
约 212 字小于 1 分钟
2026-02-11
题目
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 }解题思路
待补充
答案
type OptionalUndefined<T, Props> = any验证
import type { Equal, Expect } 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 }>>,
]参考
无
