首页 > 其他分享 >Typescript类型体操 - Chunk

Typescript类型体操 - Chunk

时间:2022-10-23 22:58:56浏览次数:82  
标签:Typescript lodash Chunk extends 体操 expected type

题目

中文

你知道 lodash 吗? lodash中有一个非常实用的方法Chunk, 让我们实现它吧.
Chunk<T, N>接受两个泛型参数, 其中 T 是 tuple 类型, N是大于 1 的数字

type exp1 = Chunk<[1, 2, 3], 2>; // expected to be [[1, 2], [3]]
type exp2 = Chunk<[1, 2, 3], 4>; // expected to be [[1, 2, 3]]
type exp3 = Chunk<[1, 2, 3], 1>; // expected to be [[1], [2], [3]]

English

Do you know lodash? Chunk is a very useful function in it, now let's implement it.
Chunk<T, N> accepts two required type parameters, the T must be a tuple, and the N must be an integer >=1

type exp1 = Chunk<[1, 2, 3], 2>; // expected to be [[1, 2], [3]]
type exp2 = Chunk<[1, 2, 3], 4>; // expected to be [[1, 2, 3]]
type exp3 = Chunk<[1, 2, 3], 1>; // expected to be [[1], [2], [3]]

答案

type Chunk<
    T extends any[],
    N extends number,
    S extends any[] = [],
    ALL extends any[] = []
> = T extends [infer L, ...infer R]
    ? S['length'] extends N
        ? Chunk<T, N, [], [...ALL, S]>
        : Chunk<R, N, [...S, L], ALL>
    : S['length'] extends 0
    ? []
    : [...ALL, S];

在线演示

标签:Typescript,lodash,Chunk,extends,体操,expected,type
From: https://www.cnblogs.com/laggage/p/type-challenge-chunk.html

相关文章

  • Typescript类型体操 - GreaterThan
    题目中文在本挑战中,你需要实现GreaterThan<T,U>,它的作用像T>U你不需要考虑负数示例:GreaterThan<2,1>//shouldbetrueGreaterThan<......
  • [Typescript] 66. Medium - IsTuple
    Implementatype IsTuple,whichtakesaninputtype T andreturnswhether T istupletype.Forexample:typecase1=IsTuple<[number]>//truetypecase2......
  • [Typescript] 65. Medium - Zip
    InThisChallenge,Youshouldimplementatype Zip<T,U>,TandUmustbe Tupletypeexp=Zip<[1,2],[true,false]>//expectedtobe[[1,true],[2,false......
  • TypeScript
    一、环境准备ts和js的区别ts属于静态类型,写代码时就能检查错误。是js的超类,包含js功能,多的是类型。js属于动态类型,只有在运行时才会报错,不会检查类型是否发生变化。......
  • [Typescript] 64. Hard - AllCombinations
    Implementtype AllCombinations<S> thatreturnallcombinationsofstringswhichusecharactersfrom S atmostonce.Forexample:typeAllCombinations_ABC=......
  • [Typescript] 63. Medium - Greater Than
    InThisChallenge,Youshouldimplementatype GreaterThan<T,U> like T>UNegativenumbersdonotneedtobeconsidered.ForexampleGreaterThan<2,1>//s......
  • TypeScript 复习进阶三部曲 (1) – 把 TypeScript 当强类型语言使用
    前言本来是想照着TypeScript官网handbook写教程的.但真的没那个mood.还是用我自己的方式写教程(其实是复习和进阶笔记)吧 学习TypeScript的三个阶段(三部......
  • TypeScript infer All In One
    TypeScriptinferAllInOnehttps://www.typescriptlang.org/docs/handbook/type-inference.htmlhttps://www.cnblogs.com/xgqfrms/tag/infer/demos//Herewedeclar......
  • [Typescript] Tips: Use assertion functions inside classes
    Youcandosomereally,reallyneatstuffwithassertionfunctionsinsideclasses.Here,weassertthattheuserisloggedinandgetproperinferenceontheu......
  • [Typescript] 62. Medium - Fibonacci Sequence
    Implementageneric Fibonacci<T> thattakesanumber T andreturnsitscorresponding Fibonaccinumber.Thesequencestarts:1,1,2,3,5,8,13,21,34,......