IsRequiredKey
约 145 字小于 1 分钟
2026-02-11
题目
Implement a generic IsRequiredKey<T, K> that return whether K are required keys of T .
For example
type A = IsRequiredKey<{ a: number, b?: string },'a'> // true
type B = IsRequiredKey<{ a: number, b?: string },'b'> // false
type C = IsRequiredKey<{ a: number, b?: string },'b' | 'a'> // false解题思路
待补充
答案
type IsRequiredKey<T, K extends keyof T> = any验证
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<IsRequiredKey<{ a: number, b?: string }, 'a'>, true>>,
Expect<Equal<IsRequiredKey<{ a: undefined, b: string }, 'a'>, true>>,
Expect<Equal<IsRequiredKey<{ a: number, b?: string }, 'b'>, false>>,
Expect<Equal<IsRequiredKey<{ a: number, b?: string }, 'b' | 'a'>, false>>,
Expect<Equal<IsRequiredKey<{ a: undefined, b: undefined }, 'b' | 'a'>, true>>,
]参考
无
