#Can I create a higher order type

6 messages · Page 1 of 1 (latest)

frosty timber
#

I need to create a higher order type that should work like this

Let's say I have a list of this type {name:string, method:string} they are all narrowed down to specific strings, if I check the type of one of them the nameormethodis of typeget` for example, now I want a type that will take in this list of objects and return either a type like this

{
  item.name:item.method
}

So I can get the method by name, or it should return another type that could get a method type by looping over the list and return the type of the method where the name extends trhe generic passed, my ideal type would look something like this

type MyType = UtilType<typeof list>

// for the first method
type SpecificMethod = MyType[name]

// for the second method
type SpecificMethod = MyType<typeof name>

I prefer option one because it will probably be better for performance, but I will use whichever one is easier

strong berry
#

!hb key remapping

onyx waveBOT
strong berry
#
type UtilType<T extends readonly { name: string, method: string }[]> = { [KV in T[number] as KV['name']]: KV['method'] }
onyx waveBOT
strong berry
#

!ts