首页 > 其他分享 >Typescript类型体操 - Tuple To Union

Typescript类型体操 - Tuple To Union

时间:2022-09-04 20:47:51浏览次数:75  
标签:Typescript Tuple Union TupleToUnion Test type

题目

中文

实现泛型TupleToUnion<T>,它返回元组所有值的合集。

例如

type Arr = ['1', '2', '3']

type Test = TupleToUnion<Arr> // expected to be '1' | '2' | '3'

English

Implement a generic TupleToUnion<T> which covers the values of a tuple to its values union.

For example

type Arr = ['1', '2', '3']

type Test = TupleToUnion<Arr> // expected to be '1' | '2' | '3'

答案

type TupleToUnion<T extends unknown[]> = T[number]

在线演示

标签:Typescript,Tuple,Union,TupleToUnion,Test,type
From: https://www.cnblogs.com/laggage/p/type-challenge-tuple-to-union.html

相关文章

  • [Typescript] 14. Easy - Parameters
    Implementthebuilt-inParametersgenericwithoutusingit.Forexample:constfoo=(arg1:string,arg2:number):void=>{}typeFunctionParamsType=MyPara......
  • [Typescript] 11. Medium - Equal
    ImplementtheEqual<T,U>Forexample:typeisEqual=Equal<1,1>//trueIdea: Parametertype: <P>(x:P)=>anyCheckPextendsT?1:2ThencheckPexte......
  • [Typescript] 12. Easy - Push
     Implementthegenericversionof Array.pushForexample:typeResult=Push<[1,2],'3'>//[1,2,'3'] /*_____________YourCodeHere_____________*/......
  • [Typescript] 13. Easy - Unshift
    Implementthetypeversionof Array.unshiftForexample:typeResult=Unshift<[1,2],0>//[0,1,2,] /*_____________YourCodeHere_____________*/t......
  • Flink 合流操作——Union
    应用中,我们经常会遇到来源不同的多条流,需要将它们的数据进行联合处理。所以Flink中合流的操作会更加普遍,对应的API也更加丰富。Union最简单的合流操作就是直接将多条流合......
  • 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......
  • [Typescript Challenges] 10. Medium - Include
    ImplementtheJavaScript Array.includes functioninthetypesystem.Atypetakesthetwoarguments.Theoutputshouldbeaboolean true or false.Forexa......