首页 > 其他分享 >[Typescript] 46. Medium - PickByType

[Typescript] 46. Medium - PickByType

时间:2022-10-14 01:45:17浏览次数:63  
标签:Typescript _____________ 46 boolean Expect Key PickByType type

From T, pick a set of properties whose type are assignable to U.

For Example

type OnlyBoolean = PickByType<{
  name: string
  count: number
  isReadonly: boolean
  isEnable: boolean
}, boolean> // { isReadonly: boolean; isEnable: boolean; }
/* _____________ Your Code Here _____________ */

type PickByType<T extends object, U> = {
  [Key in keyof T as T[Key] extends U ? Key: never]: T[Key]
}

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

interface Model {
  name: string
  count: number
  isReadonly: boolean
  isEnable: boolean
}

type cases = [
  Expect<Equal<PickByType<Model, boolean>, { isReadonly: boolean; isEnable: boolean }>>,
  Expect<Equal<PickByType<Model, string>, { name: string }>>,
  Expect<Equal<PickByType<Model, number>, { count: number }>>,
]

 

标签:Typescript,_____________,46,boolean,Expect,Key,PickByType,type
From: https://www.cnblogs.com/Answer1215/p/16790244.html

相关文章

  • [Typescript] 47. Medium- StartsWith
    Implement StartsWith<T,U> whichtakestwoexactstringtypesandreturnswhether T startswith UForexampletypea=StartsWith<'abc','ac'>//expected......
  • [Typescript] 48. Medium - EndsWith
    Implement EndsWith<T,U> whichtakestwoexactstringtypesandreturnswhether T endswith UForexample:typea=EndsWith<'abc','bc'>//expectedtob......
  • [Typescript] Abstract Classes
    Youcannotcreateainstanceofabstractclass.Anabstractclassmeantobeextended.abstractclassSize{constructor(publicsizes:string[]){}......
  • [Typescript] Get class properties type in union
    Forexamplethereisaclas:exportclassModifierState{/***Returnsthemodifierstateapplicabletothekeyboardeventgiven.*......
  • P2467 地精部落 P2885 [USACO07NOV]Telephone Wire G
    P2467SDOI2010地精部落称满足条件的序列为"波动序列"性质1:如果一个波动序列中i和i+1不相邻,则交换这两个数后仍然是波动序列性质2:将一个波动序列反转,翻转后......
  • 46. JS类型转换(强制类型转换+隐式类型转换)
    1.前言JavaScript 中有五种基本数据类型(其中包括String、Number、Boolean、Function、Symbol)、三种对象类型(其中包括Object、Date、Array)和两种特殊类型(其中包括Nul......
  • Vite + Vue3 + Pinia + es6 + TypeScript 搭建项目
    vite中文参考文档:​​https://vitejs.cn/guide/#scaffolding-your-first-vite-project​​执行 npminitvite@latest步骤如下图:下载依赖npmi 启动项目:npmrundev......
  • Luogu P3469 [POI2008]BLO-Blockade
    [P3469POI2008]BLO-Blockade-洛谷|计算机科学教育新生态(luogu.com.cn)图\(G\)本身联通。删除\(u\)的连边后会形成\(k\ge2\)个连通块(至少会把\(u\)隔离出......
  • [Typescript] 45. Easy - PickValue
    exporttypePickValue<Textendsobject,K=keyofT>=KextendskeyofT?T[K]:never;interfacePerson{name:string;address:{postcode:string;......
  • TypeScript 诞生 10 周年
    TypeScript诞生10周年来源:OSCHINA编辑: 局2022-10-1008:05:15 7TypeScript已经诞生10年了。10年前——2012年10月1日,TypeScript 首次公......