1

I would like to convert this type

type A = [interestID: string, tags: string[], text: string, creatorID: string, createdAt: number]

to the following type:

type B = {
  interestID: string,
  tags: string[],
  text: string,
  creatorID: string,
  createdAt: number
}

but I have no Idea...Is there any smart way for this?

Note: It is very similar to the following question but it is different that the elements of the pre-converted type is not fixed(changable in each array like string, string[], ...). TypeScript: tuple type to object type

kelsny
  • 23,009
  • 3
  • 19
  • 48
  • Correct. Thank you. But according to this answer, it is impossible to map without specifying each element's key name so I should think of another way of achieving goal. – Keisuke Nagakawa 永川 圭介 Nov 01 '22 at 20:47
  • 1
    Unfortunately I don't think there is any other way - TypeScript doesn't provide an intrinsic way to access the labels in the tuple. However, there is [ms/TS#41594](https://github.com/microsoft/TypeScript/issues/41594) which asks to implement one. Until then, this is the best we've got. – kelsny Nov 01 '22 at 20:49
  • 1
    Correction on my previous comment - the issue linked is not asking for a method; it's just a refactoring option. – kelsny Nov 01 '22 at 20:57

0 Answers0