首页 > 其他分享 >[Typescript] 84. Medium - GetMiddleElement

[Typescript] 84. Medium - GetMiddleElement

时间:2022-11-01 16:11:07浏览次数:48  
标签:Typescript Mid Medium extends Expect GetMiddleElement type infer

Get the middle element of the array by implementing a GetMiddleElement method, represented by an array

If the length of the array is odd, return the middle element If the length of the array is even, return the middle two elements

For example

  type simple1 = GetMiddleElement<[1, 2, 3, 4, 5]>, // expected to be [3]
  type simple2 = GetMiddleElement<[1, 2, 3, 4, 5, 6]> // expected to be [3, 4]

 

/* _____________ Your Code Here _____________ */

type GetMiddleElement<T extends any[]> = T extends [infer L, ...infer Mid, infer R]
  ? Mid['length'] extends 0
    ? [L, R]
    : Mid['length'] extends 1 | 2
      ? Mid
      : GetMiddleElement<Mid>
  : T;

// Solution 2
type GetMiddleElement<T extends any[]> = T['length'] extends 0 | 1 | 2
  ? T
  : T extends [infer L, ...infer Mid, infer R]
    ? GetMiddleElement<Mid>
    : never;


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

type cases = [
  Expect<Equal<GetMiddleElement<[]>, []>>,
  Expect<Equal<GetMiddleElement<[1, 2, 3, 4, 5]>, [3]>>,
  Expect<Equal<GetMiddleElement<[1, 2, 3, 4, 5, 6]>, [3, 4]>>,
  Expect<Equal<GetMiddleElement<[() => string]>, [() => string]>>,
  Expect<Equal<GetMiddleElement<[() => number, '3', [3, 4], 5]>, ['3', [3, 4]]>>,
  Expect<Equal<GetMiddleElement<[() => string, () => number]>, [() => string, () => number]>>,
  Expect<Equal<GetMiddleElement<[never]>, [never]>>,
]
// @ts-expect-error
type error = GetMiddleElement<1, 2, 3>

 

标签:Typescript,Mid,Medium,extends,Expect,GetMiddleElement,type,infer
From: https://www.cnblogs.com/Answer1215/p/16848069.html

相关文章

  • [Typescript] 83. Medium - Subsequence
    Givenanarrayofuniqueelements,returnallpossiblesubsequences.Asubsequenceisasequencethatcanbederivedfromanarraybydeletingsomeornoeleme......
  • typescript number 转 string(转)
    转自:number转string一、双点解析10..toString();二、括号先计算再转换(10).toString();三、加空串10+''转自:number转string......
  • [Typescript] 81. Medium - Number Range
    Sometimeswewanttolimittherangeofnumbers...Forexamples.typeresult=NumberRange<2,9>//|2|3|4|5|6|7|8|9 /*_____________Your......
  • [Typescript] 80. Medium - Construct Tuple
    Constructatuplewithagivenlength.Forexampletyperesult=ConstructTuple<2>//expecttobe[unknown,unkonwn] /*_____________YourCodeHere______......
  • typescript文件导入svelte文件报错处理办法
    在typescripts文件中引入svelte文件时报错Cannotfindmodule'../components/HelloWorld.svelte'oritscorrespondingtypedeclarations.ts(2307)并且svelte已安装......
  • TypeScript第一天学习记录
    2022年10月29日19点17分1.TypeScript是什么1.1TypeScript是什么?TypeScript(简称:TS)是JavaScript的超集(JS有的TS都有)TypeScript=Type+Javascript(在JS基础之上,为J......
  • Typescript的基本介绍
    Typescript的基本介绍0.介绍​​TypeScript​​是JavaScript的一个超集,主要提供了类型系统和对ES6的支持,它由Microsoft开发,代码​​开源于GitHub​​上。其次引用......
  • TypeScript日期工具: date-fns日期工具的使用方法
    1、引入$npminstall--savedate-fns2、使用import{isToday,isYesterday,isTomorrow,format,addYears,addMonths,addDays,addHours,add......
  • [Typescript] 79. Medium - MapTypes
    Implement MapTypes<T,R> whichwilltransformtypesinobjectTtodifferenttypesdefinedbytypeRwhichhasthefollowingstructuretypeStringToNumber=......
  • [Typescript] 78. Medium - Unqiue
    ImplementthetypeversionofLodash.uniq,UniquetakesanArrayT,returnstheArrayTwithoutrepeatedvalues.typeRes=Unique<[1,1,2,2,3,3]>;//exp......