首页 > 其他分享 >[Typescript] 12. Easy - Push

[Typescript] 12. Easy - Push

时间:2022-09-03 23:45:39浏览次数:56  
标签:12 _____________ Typescript extends Expect Easy Push type

 

Implement the generic version of Array.push

For example:

type Result = Push<[1, 2], '3'> // [1, 2, '3']

 

/* _____________ Your Code Here _____________ */

type Push<T extends any[], U> = [
  ...T,
  U
]

type Equal<T, U> = 
  (<P>(x: P) => P extends T ? 1: 2) extends 
  (<P>(x: P) => P extends U ? 1: 2) 
    ? true
    : false
/* _____________ Test Cases _____________ */
import type { Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<Push<[], 1>, [1]>>,
  Expect<Equal<Push<[1, 2], '3'>, [1, 2, '3']>>,
  Expect<Equal<Push<['1', 2, '3'], boolean>, ['1', 2, '3', boolean]>>,
]

 

标签:12,_____________,Typescript,extends,Expect,Easy,Push,type
From: https://www.cnblogs.com/Answer1215/p/16653983.html

相关文章

  • [Typescript] 13. Easy - Unshift
    Implementthetypeversionof Array.unshiftForexample:typeResult=Unshift<[1,2],0>//[0,1,2,] /*_____________YourCodeHere_____________*/t......
  • NC16122 郊区春游
    题目链接题目题目描述今天春天铁子的班上组织了一场春游,在铁子的城市里有n个郊区和m条无向道路,第i条道路连接郊区Ai和Bi,路费是Ci。经过铁子和顺溜的提议,他们决定去其中......
  • git解决The authenticity of host ‘github.com (192.30.255.112)‘ can‘t be establ
    git解决Theauthenticityofhost‘github.com(192.30.255.112)‘can‘tbeestablished问题报错完整信息Theauthenticityofhost'github.com(20.205.243.166)'c......
  • CH9121小结
    当通过网络配置工具将CH9121设置成UDPSERVER模式时,同时通过SRT-NET设置将PC设置成UDP并与CH9121用网线连接。将CH9121配置好与PC信息相匹配的端口号与IP地址,PC端相应......
  • MathProblem 80 128 pennies and a blindfold problem
    Youareblindfoldedbeforeatable.Onthetableareaverylargenumberofpennies.Youaretold128ofthepenniesareheadsupandtherestaretailsup.Ho......
  • Typescript类型体操 - Deep Readonly
    题目中文实现一个通用的DeepReadonly<T>,它将对象的每个参数及其子对象递归地设为只读。您可以假设在此挑战中我们仅处理对象。数组,函数,类等都无需考虑。但是,您仍然可以......
  • Typescript类型体操 - First of Array
    题目中文实现一个通用First<T>,它接受一个数组T并返回它的第一个元素的类型。例如:typearr1=['a','b','c']typearr2=[3,2,1]typehead1=First<arr1>//e......
  • Typescript类型体操 - Length of Tuple
    题目中文创建一个通用的Length,接受一个readonly的数组,返回这个数组的长度。例如:typetesla=['tesla','model3','modelX','modelY']typespaceX=['FALCON9'......
  • Typescript类型体操 - Tuple To Object
    题目中文传入一个元组类型,将这个元组类型转换为对象类型,这个对象类型的键/值都是从元组中遍历出来。例如:consttuple=['tesla','model3','modelX','modelY']a......
  • 124. 二叉树中的最大路径和
    124.二叉树中的最大路径和路径被定义为一条从树中任意节点出发,沿父节点-子节点连接,达到任意节点的序列。同一个节点在一条路径序列中至多出现一次。该路径至少包......