首页 > 其他分享 >[Typescript] 133. Medium - All

[Typescript] 133. Medium - All

时间:2022-12-11 00:11:06浏览次数:42  
标签:Typescript false _____________ true 133 Medium Expect type

Returns true if all elements of the list are equal to the second parameter passed in, false if there are any mismatches.

For example

type Test1 = [1, 1, 1]
type Test2 = [1, 1, 2]

type Todo = All<Test1, 1> // should be same as true
type Todo2 = All<Test2, 1> // should be same as false

 

/* _____________ Your Code Here _____________ */

type All<T extends any[], U extends any = T[0]> = T extends [infer H, ...infer RT]
  ? Equal<H, U> extends true 
    ? All<RT, U>
    : false
  : true


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

type cases = [
  Expect<Equal<All<[1, 1, 1], 1>, true>>,
  Expect<Equal<All<[1, 1, 2], 1>, false>>,
  Expect<Equal<All<["1", "1", "1"], "1">, true>>,
  Expect<Equal<All<["1", "1", "1"], 1>, false>>,
  Expect<Equal<All<[number, number, number], number>, true>>,
  Expect<Equal<All<[number, number, string], number>, false>>,
  Expect<Equal<All<[null, null, null], null>, true>>,
  Expect<Equal<All<[[1], [1], [1]], [1]>, true>>,
  Expect<Equal<All<[{}, {}, {}], {}>, true>>,
];

 

标签:Typescript,false,_____________,true,133,Medium,Expect,type
From: https://www.cnblogs.com/Answer1215/p/16972680.html

相关文章

  • sysctl_tcp_rfc1337 分析
    tcp_timewait_state_process(structinet_timewait_sock*tw,structsk_buff*skb,conststructtcphdr*th){------------------------------------------......
  • [Typescript] 131. Extreme - Query String Parser
    You'rerequiredtoimplementatype-levelparsertoparseURLquerystringintoaobjectliteraltype.Somedetailedrequirements:Valueofakeyinquerystr......
  • TypeScript 开发环境搭建
    OverridetheentrypointofanimageIntroducedinGitLabandGitLabRunner9.4.Readmoreaboutthe extendedconfigurationoptions.Beforeexplainingtheav......
  • 一. TypeScript基础
    1.TS运行环境方式一:通过webpack,配置本地的TypeScript编译环境和开启一个本地服务,可以直接运行在浏览器上方式二:通过ts-node库,为TypeScript的运行提供执行环境安装ts......
  • [Typescript] 130. Hard - Replace Union
    Givenan unionoftypes and arrayoftypepairs toreplace([[string,number],[Date,null]]),returnanewunionreplacedwiththe typepairs. /*____......
  • typeScript学习
    typeScript配置文件如果一个目录下存在一个tsconfig.json文件,那么它意味着这个目录是TypeScript项目的根目录。tsconfig.json文件中指定了用来编译这个项目的根文件和编......
  • [Typescript] 129. Hard - Capitalize Nest Object Keys
    Capitalizethekeyoftheobject,andifthevalueisanarray,iteratethroughtheobjectsinthearray. /*_____________YourCodeHere_____________*/......
  • TypeScript 之 Type
    Type描述:全称叫做'类型别名',为类型字面量提供名称。比Interface支持更丰富的类型系统特性。Type与Interface区别Interface只能描述对象的形状,Type不止Interfa......
  • TypeScript 4.9 - satisfies 操作符使用方法
    官方版本发布链接https://devblogs.microsoft.com/typescript/announcing-typescript-4-9-beta/#the-satisfies-operator新的satisfies操作符可以验证表达式的类型是否......
  • TypeScript:Type 'boolean' is not assignable to type 'never'.
    问题原因当我们声明一个空数组而不显示键入它并尝试向其中添加元素时,会发生该错误。解决方案声明数组类型即可参考链接https://bobbyhadz.com/blog/typescript-arg......