首页 > 其他分享 >[Typescript] 74. Medium - LastIndexOf

[Typescript] 74. Medium - LastIndexOf

时间:2022-10-28 15:58:21浏览次数:42  
标签:RT Typescript LastIndexOf _____________ Medium Expect Array type

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

For example:

type Res1 = LastIndexOf<[1, 2, 3, 2, 1], 2> // 3
type Res2 = LastIndexOf<[0, 0, 0], 2> // -1

 

/* _____________ Your Code Here _____________ */

type LastIndexOf<T extends any[], U> = T extends [...infer RT, infer L]
  ? Equal<L, U> extends true
    ? RT['length']
    : LastIndexOf<RT, U>
  : -1;

type z = LastIndexOf<[string, any, 1, number, 'a', any, 1], any>
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

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

 

标签:RT,Typescript,LastIndexOf,_____________,Medium,Expect,Array,type
From: https://www.cnblogs.com/Answer1215/p/16836327.html

相关文章

  • [Typescript] 72. Medium - IndexOf
    ImplementthetypeversionofArray.indexOf,indexOf<T,U>takesanArrayT,anyUandreturnstheindexofthefirstUinArrayT.typeRes=IndexOf<[1,2,3......
  • [Typescript] 71. Medium - Trunc
    Implementthetypeversionof Math.trunc,whichtakesstringornumberandreturnstheintegerpartofanumberbyremovinganyfractionaldigits.Forexample......
  • TypeScript 复习与进阶三部曲 (2) – 把 TypeScript 当编程语言使用
    前言上一篇,我们提到,TypeScript进阶有3个阶段. 第一阶段是"把TypeScript当强类型语言使用",我们已经介绍完了. 第二阶段是"把TypeScript当编程语言使用"......
  • [Typescript] 70. Medium - Without
    ImplementthetypeversionofLodash.without,Without<T,U>takesanArrayT,numberorarrayUandreturnsanArraywithouttheelementsofU.typeRes=With......
  • [Typescript] 69. Medium - Trim Right
    Implement TrimRight<T> whichtakesanexactstringtypeandreturnsanewstringwiththewhitespaceendingremoved.Forexample:typeTrimed=TrimRight<'......
  • [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是一种静态类型的弱类型......
  • [Typescript] Declare Module
    https://www.typescriptlang.org/docs/handbook/modules.html#ambient-modulesExamplefordeclarenode.js"url"&"path"module:node.d.tsdeclaremodule"url"{......
  • [Typescript] 67. Medium - Chunk
    Doyouknow lodash? Chunk isaveryusefulfunctioninit,nowlet'simplementit. Chunk<T,N> acceptstworequiredtypeparameters,the T mustbea tu......
  • [Typescript] TypeScript module Augmentation
    Youmighthavesomechangeslocallyfor3rdpartylibrary.Forthelocalimplementation,youneedtomodifythetypesinordertoresolveIDEissue. Whenme......