2021年2月10日星期三

Is it possible to change an key value interface into a tuple

I need the same data served in these two types

type DataMap = {     id001: 'name1',     id002: 'name2',     id003: 'name3',     ....  }     type DataTuple = [    {id: 'id001', name: 'name1'},    {id: 'id002', name: 'name2'},    {id: 'id003', name: 'name3'},     ...  ]  

I would like to only declare this type once and have a single source of truth, But I cannot figure out a utility type function that translates the type from an object to a tuple.

ideally I would like to do something like this:

type DataMap = {     id001: 'name1',     id002: 'name2',     id003: 'name3',     ....  };   type DataTuple = MapToTuple<DataMap>;    // or     type DataTuple = [    {id: 'id001', name: 'name1'},    {id: 'id002', name: 'name2'},    {id: 'id003', name: 'name3'},     ...  ];  type DataMap = TupleToMap<DataTuple>;  

Are either of these functions MapToTuple<T> or TupleToMap<T> possible?

https://stackoverflow.com/questions/66147572/is-it-possible-to-change-an-key-value-interface-into-a-tuple February 11, 2021 at 09:33AM

没有评论:

发表评论