00898-easy-includes

Back

// 你的答案
type Includes<T extends readonly any[], U> = T extends [infer P, ...infer Rets] ? (Equal<P, U> extends true ? true : Includes<Rets, U>): false

Solution by reonce #36049

type Includes<T extends readonly any[], U> = T extends [infer FirstEl, ...infer RestEl]
  ? Equal<FirstEl, U> extends true
      ? true
      : Includes<RestEl, U>
  : false;

Solution by karazyabko #36031

type Includes<T extends readonly any[], U> = T extends [infer First, ...infer Rest] ? Equal<First, U> extends true ? true : Includes<Rest, U> : false;

Solution by codingaring #35941

// 你的答案
// 方法1:将数组做映射,再将U作为key取出来,但它无法通过全部的测试用例
type Includes<T extends readonly any[], U> = {
  [key in T[number]]: true
}[U] extends true ? true : false

// 方法2:递归调用isEqual方法

type isEqual<A,B> = (<K>() => K extends A ? 1: 0) extends (<K>() => K extends B ? 1: 0) ? true : false
// type res = isEqual<'a', 'a'>
// type res2 = isEqual<'a', 'b'>
// type res3 = isEqual<true, boolean>
// type res4 = isEqual<null, undefined> // false
// type res5 = isEqual<undefined, null> // false

type Includes<T extends readonly any[], U> =  T extends [T[0], ...infer Rest] ?  isEqual<T[0], U> extends true ? true : Includes<Rest, U> : false

Solution by song4613107 #35794

type Includes<T extends string[], S> = S extends keyof T[number] ? true : false;

Solution by Sensuele #35778

// 你的答案
type Includes<T extends any[], U> = U extends T[number] ? true : false

Solution by ndwgg #35547

type Includes<T extends readonly any[], U> =  { [P in keyof T ]: Equal<T[P], U> extends true ? true : false } extends { [key: number]: false } ? false : true;

Solution by gangnamssal #35491

export type FirstExpression<X> = <T>() => T extends X ? 1 : 2;
export type SecondExpression<Y> = <P>() => P extends Y ? 1 : 2;
export type MyEqual<X, Y> = FirstExpression<X> extends SecondExpression<Y> ? true : false;

type Includes<T extends readonly any[], U> = true extends {
  [I in keyof T]: MyEqual<T[I], U>
}[number] ? true : false

Solution by RanungPark #35442

// your answers
type setup = ["Kars", "Esidisi", "Wamuu", "Santana"];

type Includes<T extends unknown[], U> = U extends T[number] ? true : false;

const firstData: Includes<setup, "Dio"> = false;
const trueData: Includes<setup, "Kars"> = true;

Solution by Sathiyapramod #35417

/**
Returns a boolean for whether given two types are equal.
@link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650
*/
type IsEqual<T, U> = (<G>() => G extends T ? 1 : 2) extends <G>() => G extends U
    ? 1
    : 2
    ? true
    : false;

type Includes<Value extends any[], Item> = Value extends [
    infer First,
    ...infer Rest
]
    ? IsEqual<First, Item> extends true
        ? true
        : Includes<Rest, Item>
    : false;

Solution by gyeounjeong #35360

type MyEqual<X, Y> =
  (<T>() => T extends X ? 1 : 2) extends
  (<T>() => T extends Y ? 1 : 2) ? true : false
type Includes<T extends readonly any[], U> = T extends [] ? false :
  MyEqual<T[0], U> extends true
  ? true
  : T extends [infer f, ...infer rest]
  ? Includes<rest, U>
  : false;

Solution by watanaki #35195

type IsEquar<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
type Includes<T extends readonly any[], U> = T extends [infer F, ...infer Rest]
  ? IsEquar<F, U> extends true
    ? true
    : Includes<Rest, U>
  : false;

Solution by wendao-liu #35085

My answer is inspired by util implementation of Equal but more understandable i think. It passes all given tests

type MyEquals<T,U> = 
  Readonly<T> extends T ? Readonly<U> extends T ? true : false : false;
  
type Includes<T extends readonly any[], U> = 
  T extends readonly [infer HEAD, ...infer TAIL] ? 
  Equal<HEAD,U> extends true ? true : Includes<TAIL, U> : 
    false;

Solution by CrimsoonXIII #35047

type Includes<T extends readonly unknown[], P> = P extends T[number] ? true : false

Solution by ClarityOfMind #34994

type Includes<T extends readonly any[], U> = T extends [infer First, ...infer Rest] ? (First extends U ? (U extends First ? true : false) : Includes<Rest, U>) : false;

Solution by raeyoung-kim #34957

type TupleToUnion<T extends readonly any[]> = T extends [infer First, ...infer Rest] ? First | TupleToUnion<Rest> : never;
type Includes<T extends any[], K> = K extends TupleToUnion<T> ? true : false;

We could make a TupleToUnion.

Solution by DearICE #34907

type Includes<T extends readonly any[], U> =
  Equal<U, T[0]> extends true 
  ? true
  : (T extends [any, ...infer R] 
    ? (R extends [] // if R is an empty array, the search should be terminated 
      ? false 
      : Includes<R, U>) 
    : false)

Solution by floatDreamWithSong #34871

type Includes<T extends readonly any[], K> = K extends T[number] ? true : false

Solution by weitongtong #34847

type TupleIndexes<T extends readonly any[]> = { [K in keyof T]: K }[number] 获取元组T的所有索引 type AllEqual<T extends readonly any[], U, P extends number> = P extends P ? Equal<T[P], U> : never 利用Distributive Conditional Type,如果T中所有元素都和U不相等则为false。若T恰有一个元素和U相等则为true,若T含有元素和U相等则为boolean。 type Includes<T extends readonly any[], U> = AllEqual<T, U, TupleIndexes<T>> extends false ? false : true

Solution by 2083335157 #34838

// @reference https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650
type IsEqual<X, Y> = 
  (<T>() => T extends X ? 1 : 2) extends 
  (<T>() => T extends Y ? 1 : 2) 
    ? true
    : false;

type Includes<T extends readonly any[], U> = T extends [infer First, ...infer Rest] 
  ? IsEqual<First, U> extends true 
    ? true 
    : Includes<Rest, U> 
  : false;

Solution by eunsukimme #34815

// 你的答案
type Includes<T extends readonly any[], U> = {
  [key in keyof T]: Equal<T[key], U> extends true ? true: never;
}[number] extends never ? false : true
// 借助 never 空集

Solution by Atlas-lili #34729

// your answers
type Includes<T extends readonly any[], U> = T extends [infer P, ...infer R]
  ? Equal<P, U> extends true
    ? true
    : Includes<R, U>
  : false;

Solution by zeyuanHong0 #34719

type Includes<T extends readonly any[], U> = T extends [infer I, ...infer E] ? Equal<I, U> extends true ? true : Includes<E, U> : false

Solution by nathan2slime #34662

type Includes<T extends readonly unknown[], U> =
  T extends [infer First, ...infer Rest]
    ? Equal<First, U> extends true ? true : Includes<Rest, U>
    : false;

Solution by devshinthant #34548

type Includes<T extends readonly any[], U> = 
T extends [infer Head, ... infer Tail] ? 
Equal<Head, U> extends true ? true : Includes<Tail, U>
: false

Solution by binhdv155127 #34470

// 여기 풀이를 입력하세요
type Equal<X, Y> =
  (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? true : false;

type Includes<T extends readonly any[], U> = T extends [infer F, ...infer Rest]
  ? Equal<F, U> extends true
    ? true
    : Includes<Rest, U>
  : false;

Solution by LeeKangHyun #34463

type Equal<X, Y> =
(<T>() => T extends X ? 1 : 2) extends
(<T>() => T extends Y ? 1 : 2) ? true : false;
type Includes<T extends readonly any[], U> = T extends [infer F, ...infer R] ? Equal<F, U> extends true ? true : Includes<R, U> : false

Solution by ktim816 #34427

type Includes<T extends readonly any[], U> = 
  T extends [infer F, ...infer R] 
    ? Equal<F, U> extends true
      ? true 
      : Includes<R, U> 
    : false;

Solution by bkdragon0228 #34377

type Includes<T extends readonly any[], U> = T extends [infer Fisrt, ...infer Reset]
  ? Equal<Fisrt, U> extends true ? true : Includes<Reset, U>
  : false

Solution by rookie-luochao #34363

export type IsEqual<X, Y> =
  (<T>() => T extends X ? 1 : 2) extends
  (<T>() => T extends Y ? 1 : 2) ? true : false

type Includes<T extends readonly any[], U> = T extends [infer A,...infer Reset] ? IsEqual <U,A> extends true ?true:Includes<Reset,U> : false

Solution by FrankWangMing #34340