recordの型レベルSome
定義例
code:ts
// valueが1つでもtrueのものがあればtrue
// prettier-ignore
type Some<T extends Record<string, boolean>>
= true extends IsEmpty<T>
? true // Vacuous Truth
: Not<IsEmpty<Filter<T>>>;
補足
code:ts
type Filter<T extends Record<string, any>> = {
[Key in keyof T as true extends TKey ? Key : never]: TKey; };
使用例
code:ts
type A1 = Some<{ a: true; b: true }>; // true
type A2 = Some<{ a: true; b: false }>; // true
type A3 = Some<{ a: false; b: false }>; // false
code:ts
test('Some', () => {
expectTypeOf<Some<{}>>().toEqualTypeOf<true>();
expectTypeOf<Some<{ a: true; b: true }>>().toEqualTypeOf<true>();
expectTypeOf<Some<{ a: true; b: false }>>().toEqualTypeOf<true>();
expectTypeOf<Some<{ a: false; b: false }>>().toEqualTypeOf<false>();
});