#Variable generic input length?

6 messages · Page 1 of 1 (latest)

weak kindle
#

Hello TS family,

I'd like to make a helper type with multiple generic inputs.

Ex)

type MyGenericType<GENERICS_HERE> = GENERICS_HERE[0] & GENERICS_HERE[1] & 
...

Is this possible?

upbeat coral
#

its possible by using an array and mapped types or recursion to loop over it

#
type Intersection<T> = T extends [infer Head, ...infer Tail]
  ? Head & Intersection<Tail>
  : unknown;

type X = Intersection<[{a:string}, { b: number}]>
#

!ts X

spring flameBOT
#
type X = {
    a: string;
} & {
    b: number;
} /* 5:6 */```
weak kindle
#

Thanks, @upbeat coral!