首页 > 其他分享 >类型体操-Tuple to Object

类型体操-Tuple to Object

时间:2023-04-26 15:15:43浏览次数:41  
标签:const Tuple tesla Object 体操 类型 model type TupleToObject

类型体操时,问题的记录及收获

Tuple to Object

const tuple = ['tesla', 'model 3', 'model X', 'model Y'] as const

type result = TupleToObject<typeof tuple> // expected { 'tesla': 'tesla', 'model 3': 'model 3', 'model X': 'model X', 'model Y': 'model Y'}

思路

这是issues实现的思路

Given:

type TupleToObject<T extends readonly any[]> = any

First of all we need to get rid of any[]. Input array is readonly and has only strings as values. So we can use readonly string[] type (or ReadonlyArray<string>).

type TupleToObject<T extends readonly string[]> = any

Then we should transform array literal type (['tesla', 'model 3', 'model X', 'model Y'] as const) to object type. Mapped types to the rescue. We can iterate every value in array within mapped type using in keyword.

type TupleToObject<T extends readonly string[]> = {
  [P in <somehow get T values>]: P
}

And to get the values we should use indexed access Array[number].

type TupleToObject<T extends readonly string[]> = {
  [P in T[number]]: P
}

总结

这个问题我自己做的时候,就是卡在了不知道如何获取数组的元素类型(菜狗)。即 indexed-access

通过这种方式可以获取数组元素的类型

以上加了as const,则认为是常量,拿到的是每个元素的类型

如果把as const去掉的话,则是这样的

拿到数组元素类型集合之后,再用 in 进行遍历,即可实现。

标签:const,Tuple,tesla,Object,体操,类型,model,type,TupleToObject
From: https://www.cnblogs.com/shapeY/p/17356118.html

相关文章

  • AttributeError: 'numpy.ndarray' object has no attribute 'imresize'
    问题描述:AttributeError:'numpy.ndarray'objecthasnoattribute'imresize' 分析原因:`imresize`函数已经在新版的`scipy`中被弃用,导致报错。解决办法:使用`skimage`库中的`resize`函数来代替。需要先安装`skimage`库 pipinstallscikit-image#安装skimage库fro......
  • objection使用
    Fridaobjection启动objection保证Fridaserver开启命令行输出objection-gpackageNameexploreobjection的缺陷缺少对Native层的支持对象类型的数据打印有问题,比如Bytearray和Json,会显示成[object,object]对Spawn方式启动的支持不够流畅退出exit视频演示:https://asciinema.or......
  • Java基础知识点API之Objects
    一:Objects的概述它是一个对象工具类,提供一些操作对象的方法。二:Objects的成员方法方法名说明publicstaticbooleanequals(Objecta,Objectb)先做非空判断,比较两对象publicstaticbooleanisNull(Objectobj)判断对象是否为null,为null返回true,否则返回falsepublicstaticboolea......
  • eNotInDatabase,btr.AppendEntity(attNew);必须在AddNewlyCreatedDBObject前面
     publicstaticvoidAddAttsToBlock(thisObjectIdblockId,List<AttributeDefinition>atts,boolcopyEntity=false){EntityattNew;Databasedb=blockId.Database;//获取数据库对象//打开块表记录为写......
  • Prime k-tuple UVA - 1404
        一个步骤: 在[a,b]中标记S的倍数 #include<iostream>#include<cstring>#include<algorithm>#include<vector>usingnamespacestd;constintN=1e5+20;constintQ=2e9+2;#defineintlonglongboolvis[Q];intb[N],pm[N],tot=0;......
  • Java基础知识点API之Object
    一:概述Object是java中的顶级父类,所有的父类都直接或间接地继承于Object类。Object类中的方法可以被所有子类访问,所以我们要学习Object类和其中的方法。二·:OBject的构造方法及其常用的成员方法publicObject()//空参构造classStudent(){privateStringname;private......
  • ESM export default {...object} All In One
    ESMexportdefault{...object}AllInOneobjectdestructuring&moduleexportdefaulterrosexport{...obj}❌constMetrics={autoReport,manualReport,};export{...Metrics,};exportdefaultMetrics;demosexportdefault{...obj}......
  • C 语言 共用体操作
    在C语言中,共用体是一种特殊的数据结构,它的所有成员都占用同一个内存空间,常用的共用体操作有: 1.定义共用体 定义共用体使用`union`关键字,语法格式如下: ```cunion共用体名称{  数据类型成员名称1;  数据类型成员名称2;  //...};``` 例如: ```c......
  • from collections import namedtuple, MappingImportError: cannot import name 'Mapp
     fromcollectionsimportnamedtuple,MappingImportError:cannotimportname'Mapping'from'collections'(D:\python38\Lib\collections--init__.py)出现以上错误:底层报错了,重新安装python环境,重新选择新安装的环境即可解决。 ......
  • bug|初始化项目|sass-loader报错:TypeError: this.getResolve is not a function at Ob
    Modulebuildfailed:TypeError:this.getResolveisnotafunctionatObject.loader的解决npmuninstallsass-loader(卸载当前版本)[email protected]......