The question is if it is possible to do something like that:
type a = string[] | number[] | boolean
//here i want to pick only array types from a, to make b: string[] | number[]
type b = a
But it could be of type boolean[] | number[] | string. So that should work dynamically
CodePudding user response:
Yes, you can use the Extract<T, U>
utility type to filter the union T
to only those members assignable to U
:
type A = string[] | number[] | boolean
type B = Extract<A, any[]>;
// type B = string[] | number[]
CodePudding user response:
It can be done using generics
type A = string[] | number[] | boolean
type GetArrayTypes<T> = T extends any[] ? T : never;
type b = GetArrayTypes<A>;