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

Typescript类型体操 - Type Lookup

时间:2022-09-04 23:12:09浏览次数:52  
标签:Typescript type Dog Cat LookUp interface Lookup Type breeds

题目

中文

有时,您可能希望根据某个属性在联合类型中查找类型。

在此挑战中,我们想通过在联合类型Cat | Dog中搜索公共type字段来获取相应的类型。换句话说,在以下示例中,我们期望LookUp<Dog | Cat, 'dog'>获得DogLookUp<Dog | Cat, 'cat'>获得Cat

interface Cat {
  type: 'cat'
  breeds: 'Abyssinian' | 'Shorthair' | 'Curl' | 'Bengal'
}

interface Dog {
  type: 'dog'
  breeds: 'Hound' | 'Brittany' | 'Bulldog' | 'Boxer'
  color: 'brown' | 'white' | 'black'
}

type MyDog = LookUp<Cat | Dog, 'dog'> // expected to be `Dog`

English

Sometimes, you may want to lookup for a type in a union to by their attributes.

In this challenge, we would like to get the corresponding type by searching for the common type field in the union Cat | Dog. In other words, we will expect to get Dog for LookUp<Dog | Cat, 'dog'> and Cat for LookUp<Dog | Cat, 'cat'> in the following example.

interface Cat {
  type: 'cat'
  breeds: 'Abyssinian' | 'Shorthair' | 'Curl' | 'Bengal'
}

interface Dog {
  type: 'dog'
  breeds: 'Hound' | 'Brittany' | 'Bulldog' | 'Boxer'
  color: 'brown' | 'white' | 'black'
}

type MyDogType = LookUp<Cat | Dog, 'dog'> // expected to be `Dog`

答案

type LookUp<U extends {}, T extends string> = U extends { type: infer J } ? J extends T ? U : never : never;

标签:Typescript,type,Dog,Cat,LookUp,interface,Lookup,Type,breeds
From: https://www.cnblogs.com/laggage/p/type-challenge-look-up.html

相关文章

  • 终止 Array.prototype.forEach 方法运行的方式
    通常情况下,Array.prototype上的遍历方法forEach、map、filter、...被调用以后会完整的遍历每一个数组项,并执行内部代码指令,无法被中途终止。但是可以通过 throw语句......
  • Typescript类型体操 - Chainable Options
    题目中文在JavaScript中我们经常会使用可串联(Chainable/Pipeline)的函数构造一个对象,但在TypeScript中,你能合理的给它赋上类型吗?在这个挑战中,你可以使用任意你喜欢的......
  • Typescript类型体操 - Tuple To Union
    题目中文实现泛型TupleToUnion<T>,它返回元组所有值的合集。例如typeArr=['1','2','3']typeTest=TupleToUnion<Arr>//expectedtobe'1'|'2'|'3'Eng......
  • freetype矢量字体 —— 介绍篇
    目录矢量字体什么是矢量字体?字体分类字体文件扩展名freetype字体引擎文字的显示过程freetype显示文字流程使用freetype显示单个文字使用wchar_t表示字符的UNICODE值使用fre......
  • [Go] Types - Reflect.TypeOf()
    Canusereflect.TypeOftogetvariabletypepackagemainimport( "fmt" "reflect")funcmain(){ //varageint=21 //varbbool=age>=23 varage......
  • Mybatis的ResultMap和ResultType的区别
    在使用mybatis进行数据库连接操作时对于SQL语句返回结果的处理通常有两种方式,一种就是resultType另一种就是resultMap,下面说下我对这两者的认识和理解resultType:当使用re......
  • [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......