首页 > 其他分享 >[Typescript] 72. Medium - IndexOf

[Typescript] 72. Medium - IndexOf

时间:2022-10-27 14:44:05浏览次数:46  
标签:Typescript _____________ IndexOf extends 72 expected type Expect

Implement the type version of Array.indexOf, indexOf<T, U> takes an Array T, any U and returns the index of the first U in Array T.

type Res = IndexOf<[1, 2, 3], 2>; // expected to be 1
type Res1 = IndexOf<[2,6, 3,8,4,1,7, 3,9], 3>; // expected to be 2
type Res2 = IndexOf<[0, 0, 0], 2>; // expected to be -1

 

/* _____________ Your Code Here _____________ */
export type Equal<T, U> = 
  (<P>(x: P) => P extends T ? 1: 2) extends 
  (<P>(x: P) => P extends U ? 1: 2) 
    ? true
    : false;
export type IndexOf<T extends any[], U, ACC extends unknown[] = []> = T extends [infer F, ...infer RT]
  ? Equal<F, U> extends true
    ? ACC['length']
    : IndexOf<RT, U, [...ACC, unknown]>
  : -1;


/* _____________ Test Cases _____________ */
import type { Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<IndexOf<[1, 2, 3], 2>, 1>>,
  Expect<Equal<IndexOf<[2, 6, 3, 8, 4, 1, 7, 3, 9], 3>, 2>>,
  Expect<Equal<IndexOf<[0, 0, 0], 2>, -1>>,
  Expect<Equal<IndexOf<[string, 1, number, 'a'], number>, 2>>,
  Expect<Equal<IndexOf<[string, 1, number, 'a', any], any>, 4>>,
  Expect<Equal<IndexOf<[string, 'a'], 'a'>, 1>>,
  Expect<Equal<IndexOf<[any, 1], 1>, 1>>,
]

 

标签:Typescript,_____________,IndexOf,extends,72,expected,type,Expect
From: https://www.cnblogs.com/Answer1215/p/16832187.html

相关文章

  • [Typescript] 71. Medium - Trunc
    Implementthetypeversionof Math.trunc,whichtakesstringornumberandreturnstheintegerpartofanumberbyremovinganyfractionaldigits.Forexample......
  • 1723. 完成所有工作的最短时间
    题目描述给了一个整数数组jobs表示工作,元素ei表示第i个工作需要花费的时间给k个人,每个工作都需要分配,且每个只能给一个人问如果要完成所有工作,求最短的工作时间?f1-状态......
  • TypeScript 复习与进阶三部曲 (2) – 把 TypeScript 当编程语言使用
    前言上一篇,我们提到,TypeScript进阶有3个阶段. 第一阶段是"把TypeScript当强类型语言使用",我们已经介绍完了. 第二阶段是"把TypeScript当编程语言使用"......
  • Codeforces 1672 E. notepad.exe
    题意这是一道交互题,有n个字符串,每个字符串长度:0-2000,n:0-2000有一个机器对他进行排版,你可以给他一个每行的最大宽度w,那么每行只能放长度为w的字符;每行相邻两个字符......
  • [Typescript] 70. Medium - Without
    ImplementthetypeversionofLodash.without,Without<T,U>takesanArrayT,numberorarrayUandreturnsanArraywithouttheelementsofU.typeRes=With......
  • leetcode-728-easy
    SelfDividingNumbersAself-dividingnumberisanumberthatisdivisiblebyeverydigititcontains.Forexample,128isaself-dividingnumberbecause128......
  • [Typescript] 69. Medium - Trim Right
    Implement TrimRight<T> whichtakesanexactstringtypeandreturnsanewstringwiththewhitespaceendingremoved.Forexample:typeTrimed=TrimRight<'......
  • POJ4072多点共线问题
    有N(1<=n<=100)<span="">个互不重合的点,并给出它们的坐标(xi,yi),问这些点是否在同一直线上。输入第一行是测试的组数T(1<=T<=100),其后是T组数据,每组数据第一行是该......
  • [Typescript] 68. Medium - Fill
    Fill,acommonJavaScriptfunction,nowletusimplementitwithtypes. Fill<T,N,Start?,End?>,asyoucansee,Fill acceptsfourtypesofparameters,ofwh......
  • #yyds干货盘点#初聊typescript
    一、认识Typescript(1)Javascript是一种动态类型的弱类型语言Javascript超集:A.包含与兼容所有JS特性,支持共存B.支持渐进式引入与升级(2)TypeScript是一种静态类型的弱类型......