首页 > 其他分享 >[Typescript] 68. Medium - Fill

[Typescript] 68. Medium - Fill

时间:2022-10-26 01:11:05浏览次数:65  
标签:Typescript End parameters Start Medium extends 68 type Fill

Fill, a common JavaScript function, now let us implement it with types. Fill<T, N, Start?, End?>, as you can see,Fill accepts four types of parameters, of which T and N are required parameters, and Start and End are optional parameters. The requirements for these parameters are: T must be a tupleN can be any type of value, Start and End must be integers greater than or equal to 0.

type exp = Fill<[1, 2, 3], 0> // expected to be [0, 0, 0]
type PlusOne<T extends number, C extends unknown[] = []> = C['length'] extends T
  ? [...C, unknown]['length']
  : PlusOne<T, [...C, unknown]>
type Fill<
  T extends unknown[],
  N,
  Start extends number = 0,
  End extends number = T['length'],
  P extends number = 0                                                // set a pointer, init with 0
> = Start extends End                                                 // Stop when Start = End
  ? T
  : T extends [infer F, ...infer RT] 
    ? P extends Start                  
      ? [N, ...Fill<RT, N, PlusOne<Start>, End, PlusOne<P>>]          // Replace the value when Pointer = Start, increasing value by 1
      : [F, ...Fill<RT, N, Start, End, PlusOne<P>>]                   // No need to replace F, increase P
    : [];

 

标签:Typescript,End,parameters,Start,Medium,extends,68,type,Fill
From: https://www.cnblogs.com/Answer1215/p/16826968.html

相关文章

  • #yyds干货盘点#初聊typescript
    一、认识Typescript(1)Javascript是一种动态类型的弱类型语言Javascript超集:A.包含与兼容所有JS特性,支持共存B.支持渐进式引入与升级(2)TypeScript是一种静态类型的弱类型......
  • AcWing168 生日蛋糕(剪枝)
    原题链接思路的话,就是暴搜加剪枝,中间有个放缩思路看这篇#include<bits/stdc++.h>#definepbpush_back#definefifirst#definesesecond#defineall(x)(x).begi......
  • Luogu P4868 Preprefix sum
    题目链接:​​传送门​​线段树维护前缀和简单明了修改就修改当然还有更快的树状数组差分的做法*/#include<iostream>#include<cstdio>#include<cstring>#include<cs......
  • CF685B Kay and Snowflake
    题目链接:​​传送门​​给出q组询问每次求以这个点为根的子树的重心,n,q<=300000树的重心的一个性质:每棵的子树的大小都不超过整个树大小的一半具体细节看代码实现*/#incl......
  • [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......
  • Typescript类型体操 - Trunc
    题目中文实现类型版本的Math.trunc,其接受一个字符串或数字作为泛型参数,并返回移除了全部小数位部分后的整数示例:typeA=Trunc<12.34>;//12EnglishImpleme......
  • Typescript类型体操 - TrimRight
    题目中文实现TrimRight<T>,它接收确定的字符串类型并返回一个新的字符串,其中新返回的字符串删除了原字符串结尾的空白字符串。例如typeTrimed=TrimRight<'Hello......
  • TypeScript 高级类型
    一、高级类型class类型兼容性交叉类型泛型和keyof索引签名类型和索引查询类型映射类型二、class构造函数给变量赋值extends继承implement实现接口//感叹......