首页 > 其他分享 >[Typescript] 70. Medium - Without

[Typescript] 70. Medium - Without

时间:2022-10-26 21:00:41浏览次数:64  
标签:Typescript _____________ Medium extends Expect array type Without

Implement the type version of Lodash.without, Without<T, U> takes an Array T, number or array U and returns an Array without the elements of U.

type Res = Without<[1, 2], 1>; // expected to be [2]
type Res1 = Without<[1, 2, 4, 1, 5], [1, 2]>; // expected to be [4, 5]
type Res2 = Without<[2, 3, 2, 3, 2, 3, 2, 3], [2, 3]>; // expected to be []

 

/* _____________ Your Code Here _____________ */

type Without<T extends any[], U extends any[] | any> = T extends [infer F, ...infer RT]
  ? U extends any[]                 // U is array like
    ? F extends U[number]           // F in U array
      ? Without<RT, U>                // exlcude F
      : [F, ...Without<RT, U>]        // include F
    : F extends U                   // U is not an array like
      ? Without<RT, U>                // exlcude F
      : [F, ...Without<RT, U>]        // include F
  : [];

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

type cases = [
  Expect<Equal<Without<[1, 2], 1>, [2]>>,
  Expect<Equal<Without<[1, 2, 4, 1, 5], [1, 2]>, [4, 5]>>,
  Expect<Equal<Without<[2, 3, 2, 3, 2, 3, 2, 3], [2, 3]>, []>>,
]

 

标签:Typescript,_____________,Medium,extends,Expect,array,type,Without
From: https://www.cnblogs.com/Answer1215/p/16830018.html

相关文章

  • [Typescript] 69. Medium - Trim Right
    Implement TrimRight<T> whichtakesanexactstringtypeandreturnsanewstringwiththewhitespaceendingremoved.Forexample:typeTrimed=TrimRight<'......
  • [Typescript] 68. Medium - Fill
    Fill,acommonJavaScriptfunction,nowletusimplementitwithtypes. Fill<T,N,Start?,End?>,asyoucansee,Fill acceptsfourtypesofparameters,ofwh......
  • #yyds干货盘点#初聊typescript
    一、认识Typescript(1)Javascript是一种动态类型的弱类型语言Javascript超集:A.包含与兼容所有JS特性,支持共存B.支持渐进式引入与升级(2)TypeScript是一种静态类型的弱类型......
  • [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实现接口//感叹......
  • React-hooks+TypeScript最佳实战
    ReactHooks什么是HooksReact一直都提倡使用函数组件,但是有时候需要使用state或者其他一些功能时,只能使用类组件,因为函数组件没有实例,没有生命周期函数,只有类组件才......