'Typescript cast array of object to a linear type

I'm a beginner in the fabulous world of type creation in typescript and I have a little problem.

I have a array of obects

type foo = [
  {name:"aa", type:"string", usless:"cc", ...},
  {name:"aaaa", type:"number", ...},
  {name:"aaa", usless:"cc", ...}
]

my goal is to transform my type foo to a reduce one

type reduceFoo = transformArray<foo>
//    ^? {name:"aa",type:string}|{name:"aaaa",type:number}|{name:"aaa"}

One of the tricky things is that when the type attribute is not defined, I want it not to be in the final type.

I tried to loop on foo with {[U in foo]:Pick<U,'name'|'type'>} but this is not the right way to do it

And finally I have a function that transforms a string into a type (stringToType<T:string>) but I want to apply it only on the type attribute of the object. But I have no idea when to call him



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source