首页 > 其他分享 >[Typescript] 78. Medium - Unqiue

[Typescript] 78. Medium - Unqiue

时间:2022-10-29 16:23:39浏览次数:40  
标签:Typescript _____________ Unqiue Medium extends Expect expected Unique type

Implement the type version of Lodash.uniq, Unique takes an Array T, returns the Array T without repeated values.

type Res = Unique<[1, 1, 2, 2, 3, 3]>; // expected to be [1, 2, 3]
type Res1 = Unique<[1, 2, 3, 4, 4, 5, 6, 7]>; // expected to be [1, 2, 3, 4, 5, 6, 7]
type Res2 = Unique<[1, "a", 2, "b", 2, "a"]>; // expected to be [1, "a", 2, "b"]
type Res3 = Unique<[string, number, 1, "a", 1, string, 2, "b", 2, number]>; // expected to be [string, number, 1, "a", 2, "b"]
type Res4 = Unique<[unknown, unknown, any, any, never, never]>; // expected to be [unknown, any, never]

 

/* _____________ Your Code Here _____________ */

export type Equal<T, U> = 
  (<P>(x: P) => P extends T ? 1: 2) extends 
  (<P>(x: P) => P extends U ? 1: 2) 
    ? true
    : false;

export type IndexOf<T extends any[], U, ACC extends unknown[] = []> = T extends [infer F, ...infer RT]
  ? Equal<F, U> extends true
    ? ACC['length']
    : IndexOf<RT, U, [...ACC, unknown]>
  : -1;

export type Unique<T extends any[], ACC extends any[] = []> = T extends [infer F, ...infer RT]
  ? IndexOf<ACC, F> extends -1
    ? Unique<RT, [...ACC, F]>
    : Unique<RT, ACC>
  : ACC;

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

type cases = [
  Expect<Equal<Unique<[1, 1, 2, 2, 3, 3]>, [1, 2, 3]>>,
  Expect<Equal<Unique<[1, 2, 3, 4, 4, 5, 6, 7]>, [1, 2, 3, 4, 5, 6, 7]>>,
  Expect<Equal<Unique<[1, 'a', 2, 'b', 2, 'a']>, [1, 'a', 2, 'b']>>,
  Expect<Equal<Unique<[string, number, 1, 'a', 1, string, 2, 'b', 2, number]>, [string, number, 1, 'a', 2, 'b']>>,
  Expect<Equal<Unique<[unknown, unknown, any, any, never, never]>, [unknown, any, never]>>,
]

 

标签:Typescript,_____________,Unqiue,Medium,extends,Expect,expected,Unique,type
From: https://www.cnblogs.com/Answer1215/p/16838974.html

相关文章

  • 【TypeScript】语法精讲
    TypeScript的编译环境#安装命令npminstalltypescript-g#查看版本tsc--versionTypeScript的运行环境#安装ts-nodenpminstallts-node-g#另外ts-node需要依赖tslib......
  • typeScript基础语法
    1.变量定义变量名:变量的类型(首字母小写)letdan:string='蛋'letnumArr:number[]=[4,2,0];2、核心定义任何东西的时候要注明类型调用任何东西的时候要检查类型3......
  • [Typescript] 75. Easy - Push
    Implementthegenericversionof Array.pushForexample:typeResult=Push<[1,2],'3'>//[1,2,'3'] /*_____________YourCodeHere_____________*/t......
  • [Typescript] 77. Easy - Unshift
    Implementthetypeversionof Array.unshiftForexample:typeResult=Unshift<[1,2],0>//[0,1,2,] /*_____________YourCodeHere_____________*/t......
  • [Typescript] 76. Easy - Parameters
    Implementthebuilt-inParametersgenericwithoutusingit.Forexample:constfoo=(arg1:string,arg2:number):void=>{}typeFunctionParamsType=MyPara......
  • [Typescript] 74. Medium - LastIndexOf
    Implementthetypeversionof Array.lastIndexOf, LastIndexOf<T,U> takesanArray T,any U andreturnstheindexofthelast U inArray TForexample:......
  • [Typescript] 72. Medium - IndexOf
    ImplementthetypeversionofArray.indexOf,indexOf<T,U>takesanArrayT,anyUandreturnstheindexofthefirstUinArrayT.typeRes=IndexOf<[1,2,3......
  • [Typescript] 71. Medium - Trunc
    Implementthetypeversionof Math.trunc,whichtakesstringornumberandreturnstheintegerpartofanumberbyremovinganyfractionaldigits.Forexample......
  • TypeScript 复习与进阶三部曲 (2) – 把 TypeScript 当编程语言使用
    前言上一篇,我们提到,TypeScript进阶有3个阶段. 第一阶段是"把TypeScript当强类型语言使用",我们已经介绍完了. 第二阶段是"把TypeScript当编程语言使用"......
  • [Typescript] 70. Medium - Without
    ImplementthetypeversionofLodash.without,Without<T,U>takesanArrayT,numberorarrayUandreturnsanArraywithouttheelementsofU.typeRes=With......