Reputation: 4333
Since TypeScript 4.0, we have named tuples, which is great:
type Fn = (foo: number) => void;
type Foo = Parameters<Fn>;
// type Foo = [foo: number];
However, I am facing a situation in which I now have a named tuple but the names are just clutter. I don't want the names.
How can I build a RemoveNames<T>
utility type that will take [foo: number]
and return [number]
?
Note: of course, I need it to work with tuples of any size.
Upvotes: 3
Views: 625
Reputation: 329808
Those tuple names tend to be pretty sticky, but they do disappear if you prepend/append a non-named element to the tuple. So one way to do this could be to prepend and then remove some type, like this:
type RemoveNames<T extends any[]> = [any, ...T] extends [any, ...infer U] ? U : never
That seems to act the way you want:
type UnnamedFoo = RemoveNames<Foo>;
// type UnnamedFoo = [number]
Upvotes: 5