00949-medium-anyof

Back

type IsEmptyObject<T> = [T] extends [{}] ? keyof T extends never ? true : false : false

type Falsy = 0 | '' | false | undefined | null | []
type IsFalsy<T> = [T] extends [Falsy] 
  ? true 
  : IsEmptyObject<T> extends true ? true : false;

type AnyOf<T extends readonly any[]> = T extends [infer First, ...infer Rest] 
  ? IsFalsy<First> extends true
    ? AnyOf<Rest>
    : true
  : false

Solution by eunsukimme #35243

type Falsy = false | 0 | '' | [] | Record<string, never> | undefined | null;
type IsNever<T> = [T] extends [never] ? true : false;
type IsFalsy<T> = IsNever<T> extends true
  ? true
  : T extends Falsy
  ? true
  : false;
type AnyOf<T extends readonly any[]> = T extends [infer F, ...infer Rest] ? IsFalsy<F> extends true ? AnyOf<Rest> : true : false

Solution by wendao-liu #35172

I read some answers and I dont think they can be a real answer becase

Here is my answer

type AnyOf<T extends readonly any[]> = T['length'] extends 0
  ? false
  : T[number] extends true | NonNullable<Object>
    ? true
    : false

Solution by Yaphet2015 #34993

// 你的答案
type AnyOf<T extends readonly any[]> = T extends [infer A, ...infer Rest]
  ? A extends 0 | "" | [] | { [key: string]: never } | false | undefined | null
    ? AnyOf<Rest>
    : true
  : false;

Solution by shx123qwe #34984

// your answers
type Falsy = 0 | '' | false | undefined | null | []
type AnyOf<T extends readonly any[]> = T extends [infer First, ...infer Rest]
 ? First extends Falsy
    ? AnyOf<Rest>
    : keyof First extends never
      ? false
      : true
 : false

Solution by AndreGeng #34748

type Falsy = 0 | '' | false | [] | { [K in any]: never } | undefined | null;

type AnyOf<T extends readonly any[]> = T[number] extends Falsy ? false : true;

Solution by devshinthant #34615

// 你的答案

type AnyOf<T extends any[]> = 
T[number] extends '' | false | 0 | [] | {[key: string]: never} | undefined | null ? 
false : true;


Solution by Jayce-liang #34296

type AnyOf<T extends readonly any[]> = T[number] extends ('' | false | 0 | [] | {[key: string]: never} | undefined | null) ? false : true

Solution by ouzexi #34027

Bit different than other solutions that I saw. It also covers the problematic case that a lot of the older solutions don't get: Expect<Equal<AnyOf<[0, '', false, [], {}, undefined, null]>, false>>,

type AnyOf<T extends readonly any[]> = T extends [infer First, ...infer Rest] 
  ? First extends 0 | '' | false | undefined | [] | {[key: string]: never} | null
    ? AnyOf<Rest> : true
  : false

Solution by netr #33938

// 你的答案
type IsFalsy<T> =
  T extends false | 0 | '' | [] | { [K in any]: never } | undefined | null
    ? true
    : false;

type IsTrue<T> = IsFalsy<T> extends true ? false : true;

type Tuple2Union<T> = T extends any[] ? T[number] : T;

type AnyOf<T extends readonly any[]> = IsTrue<Tuple2Union<T>> extends true ? true : false;

Solution by HelloGGG #33419

// 你的答案
type A = 0 | "" | false | [] | { [key: string]: never };

type AnyOf<T extends Array<any>> = T extends [infer F, ...infer S] ? (F extends A ? AnyOf<S> : true) : false;

Solution by 2531800823 #33243

麻烦的解法,难点在于怎么组合空数组和空对象,然后非空打上true标记,空打上false标记,提取标记的数组类型再比较

type CodValue = 0 | '' | undefined | null  | false;
type EmptyArray<T> = T extends any[] ? T['length'] extends 0 ? true : false : false;
type EmptyObject<T> = keyof T extends [never] ? true : false;
type Empty<T> = T extends any[] ? EmptyArray<T> : EmptyObject<T>;


type AnyOf<T extends readonly any[], A extends boolean[] = []> =  T extends [infer F, ...infer R] ?  
    Empty<F> extends true ? AnyOf<R, [...A, false]> :  F extends CodValue ? AnyOf<R, [...A, false]> : AnyOf<R, [...A, true]> : A['length'] extends 0 ? false : A extends Array<infer U> 
    ? boolean extends U ? true : U : never;

Solution by rootdavid #32613

// your answers
type AnyOf<T extends any[]> = T[number] extends 0  | '' | false | [] | { [key: string]: never } | undefined | null ? false: true

Solution by pea-sys #32520

type IsFalsy<V> = V extends 0 | '' | false | [] | Record<keyof any, never> | undefined | null ? true : false
type AnyOf<T extends readonly unknown[]> = 
  T extends [infer V, ...infer R] 
    ? IsFalsy<V> extends true
      ? AnyOf<R> 
      : true
    : false

더 간결한 풀이

type AnyOf<T extends any[]> = T[number] extends 0 | '' | false | [] | {[key: string]: never}
? false : true;

Solution by dev-hobin #32419

type AnyOf < T extends readonly any[]> = { [key in keyof T]: T[key] extends False ? false : keyof T[key] extends never ? false :true }

Solution by T-33 #32037

type AnyOf<T extends readonly any[]> = T extends [infer First, ...infer Rest]
  ? First extends 0 | '' | false | [] | {} | undefined | null
    ? AnyOf<Rest>
    : true
  : false;

Solution by jinyoung234 #31966


type AnyOf<T extends readonly unknown[]> = undefined extends UnionFromTuple<T>
	? false
	: null extends UnionFromTuple<T>
	? false
	: UnionFromTuple<T> extends never
	? false
	: true;

Solution by gasmg #31895

type AnyOf<T extends readonly any[]> = T extends [infer A, ...infer R] ? 
  [{}] extends [A] ?
    AnyOf<R> 
    : [A] extends [0 | '' | false | [] | null | undefined] ? 
        R extends [] ? 
          false : 
          AnyOf<R>
      : true
: false;

Solution by jay0815 #31762

// 你的答案
type AnyOf<T extends readonly any[]> = T extends [infer F, ...infer Rest]
                                      ? F extends 0 | '' | false | [] | {[key:string]:never} | undefined | null
                                        ? AnyOf<Rest>
                                        : true
                                      : false

Solution by kingshdu #31697

type AnyOf<T extends readonly any[]> = T extends [infer F, ...infer Rest] 
  ? Equal<F, {}> extends true ? AnyOf<Rest> : F extends Falsy ? AnyOf<Rest> : true  
  : false;

Solution by kai-phan #31660

// 你的答案
type AnyOf<T extends readonly any[]> =
T extends [infer F, ...infer R]
  ? F extends false
    ? AnyOf<R>
    : true
  : false 

但是并不能通过这条case

Expect<Equal<AnyOf<[0, '', false, [], {}, undefined, null]>, false>

这条case与描述不符,试了其他题解也无法通过……

seems this case is wrong

Solution by L0st1 #31613


type isTrue<T, K = keyof T> = 
    T extends Boolean
    ? T
    : [T] extends [never]
        ? false
        : T extends `${infer S}` 
            ? S extends '' 
                ? false 
                : true 
            : T extends number
                ? T extends 0
                    ? false
                    : true
                : T extends Array<infer U>
                    ? [U] extends [never]
                        ? false
                        : true
                    : T extends Object
                        ? [K] extends [never]
                            ? false
                            : true
    : false;

type AnyOf<T extends any[]> = 
    T extends [infer E, ...infer Rest]
        ? isTrue<E> extends true
            ? true
            : AnyOf<Rest>
    : false;

Solution by ricky-fn #31437

// your answers

// define falsy type
type Falsy = false | 0 | -0 | "" | null | [] | undefined | { [key: string]: never };

// destruct element and judge
type AnyOf<T extends readonly any[]> = T extends [infer F, ...infer R]
  ? F extends Falsy ? AnyOf<R> : true
  : false;

Solution by d1zzzzy #31243

// your answers
type AnyOf<T extends readonly any[]> = T[number] extends 0 | false | '' | undefined | null | [] | Record<string, never> ? false : true;

Solution by wzc520pyfm #31132

// 你的答案
type Falsy = 0 | '' | never | false | [] | Record<any, never> | null | undefined;
type AnyOf<T extends readonly any[]> = T extends Falsy[] ? false : true;

Solution by CDSP98 #31033

// https://github.com/type-challenges/type-challenges/blob/main/questions/00949-medium-anyof/README.md
type IsTrue<T> = T extends 0 | "" | false | [] | undefined
  ? false
  : T extends {}
  ? keyof T extends never
    ? false
    : true
  : true;

type AnyOf<T extends readonly unknown[]> = T extends [infer F, ...infer R]
  ? IsTrue<F> extends true
    ? true
    : AnyOf<R>
  : IsTrue<T[0]>;

Solution by flt3150sk #30885

This is my solution, although it's a bit rigid, at least it can work. Can someone guide me.

type AnyOf<T extends readonly any[]> = T extends [infer L, ...infer R]
  ? IsFalse<L> extends false
    ? AnyOf<R>
    : true
  : false

type IsFalse<F> = F extends false
  ? false
  : F extends 0
  ? false
  : F extends ''
  ? false
  : F extends []
  ? false
  : keyof F extends keyof {}
  ? false
  : F extends undefined
  ? false
  : F extends null
  ? false
  : true

/**
 * 0 => false
 * '' => false
 * false => false
 * [] => false
 * {} => false
 * undefined => false
 * null => false
 */

Solution by Chan-Yuxi #30596

// ============= Test Cases ============= import type { Equal, Expect } from "./test-utils";

type cases = [ Expect< Equal<AnyOf<[1, "test", true, [1], { name: "test" }, { 1: "test" }]>, true>

, Expect<Equal<AnyOf<[1, "", false, [], {}]>, true>>, Expect<Equal<AnyOf<[0, "test", false, [], {}]>, true>>, Expect<Equal<AnyOf<[0, "", true, [], {}]>, true>>, Expect<Equal<AnyOf<[0, "", false, [1], {}]>, true>>, Expect<Equal<AnyOf<[0, "", false, [], { name: "test" }]>, true>>, Expect<Equal<AnyOf<[0, "", false, [], { 1: "test" }]>, true>>, Expect< Equal<AnyOf<[0, "", false, [], { name: "test" }, { 1: "test" }]>, true> , Expect<Equal<AnyOf<[0, "", false, [], {}, undefined, null]>, false>>, Expect<Equal<AnyOf<[]>, false>> ];

// ============= Your Code Here ============= type AnyOf<T extends readonly any[]> = T[number] extends | "" | 0 | false | null | undefined | [] | Record<string, never> ? false : true;

Solution by z-w-H #30470

type Falsy = false | 0 | '' | undefined | null | [] | Record<string, never>;

type AnyOf<T extends readonly any[]> = T extends [infer F, ...infer Rest] ? (F extends Falsy ? AnyOf<Rest> : true) : false;

Solution by kai-phan #30389

type FalsyArray = 0 | '' | false | [] | Record<string, never> | undefined | null; type AnyOf<T extends readonly any[]> = T extends FalsyArray[] ? false : true

Solution by Miodan #30327