首页 > 其他分享 >Typescript类型体操 - Unique

Typescript类型体操 - Unique

时间:2022-11-12 03:11:05浏览次数:71  
标签:Typescript Contains extends 体操 expected Unique type infer

题目

中文

实现类型的 Lodash.uniq, Unique 接受数组 T, 返回没有重复值的数组 T

English

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]

答案

type Contains<TArr extends any[], T> = TArr extends [infer L, ...infer R]
    ? (<F>() => F extends L ? 1 : 0) extends <F>() => F extends T ? 1 : 0
        ? true
        : Contains<R, T>
    : false;

type Unique<T extends any[], O extends any[] = []> = T extends [
    infer L,
    ...infer R
]
    ? Contains<O, L> extends true
        ? Unique<R, O>
        : [L, ...Unique<R, [...O, L]>]
    : [];

这题的主要问题和 05317-medium-lastindexof 一样, 就是判断两个类型是否完全相同, type-challenge-last-index-of; 这个问题解决了剩下的就是简单的递归了

在线演示

标签:Typescript,Contains,extends,体操,expected,Unique,type,infer
From: https://www.cnblogs.com/laggage/p/type-challenge-unique.html

相关文章

  • Typescript类型体操 - LastIndexOf
    题目中文实现类型的Array.lastIndexOf,LastIndexOf<T,U>接受泛型参数ArrayT和anyU并返回数组T中最后一个U的索引示例:typeRes1=LastIndexOf<[1,2,3......
  • 【TS】1103- 30个小知识让你更清楚TypeScript
    TypeScript是Microsoft开发的JavaScript的开源超集,用于在不破坏现有程序的情况下添加附加功能。由于其独特的优势,例如,静态类型和许多速记符号,TypeScript现在被前端和......
  • [Typescript] Zod in actions
    import{z}from"zod";exportenumSUBTYPE{ABORT="abort",START="start",UPLOAD="upload",LOADING="loading",}exportconstTYPE="print"......
  • typescript装饰器
    属性装饰器参数exportdefaultfunction(proto,key){//两个参数}给属性增加metadataimport'reflect-metadata';exportdefaultfunction(label,type?......
  • [Typescript] 97. Hard - Capitalize Words
    Implement CapitalizeWords<T> whichconvertsthefirstletterof eachwordofastring touppercaseandleavestherestas-is.Forexampletypecapitalized......
  • 安装 TypeScript 并编译成JS
    官网:https://github.com/microsoft/TypeScriptTypeScript是一种由微软开发的开源、跨平台的编程语言。它是JavaScript的超集,最终会被编译为JavaScript代码。TypeScript......
  • OJ中Typescript语法整理
    基础原始类型原始类型:number/string/boolean/null/undefined/symbol对象类型:oject(数组,对象,函数等)自定义复杂的对象类型:typeCustomArray=(number|string)let......
  • [Typescript] 95. Hard - Required Keys
    Implementtheadvancedutiltype RequiredKeys<T>,whichpicksalltherequiredkeysintoaunion.ForexampletypeResult=RequiredKeys<{foo:number;bar?:......
  • [Typescript] 96. Hard - Optional Keys
    Implementtheadvancedutiltype OptionalKeys<T>,whichpicksalltheoptionalkeysintoaunion. /*_____________YourCodeHere_____________*/typeOp......
  • typescript 泛型
    一、泛型与any类型的区别泛型是等待确定的占位类型,可以理解为函数的形参;所以泛型是固定的某一个类型,实例化的时候确定其实际类型any类型是顶级类型,它包括了所有的基......