Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: ArrayTail #135

Closed
rifler opened this issue Oct 21, 2020 · 5 comments · Fixed by #913
Closed

Proposal: ArrayTail #135

rifler opened this issue Oct 21, 2020 · 5 comments · Fixed by #913
Labels
help wanted Extra attention is needed type addition

Comments

@rifler
Copy link

rifler commented Oct 21, 2020

type Tail<T extends any[]> = ((...args: T) => any) extends ((_: any, ...args: infer P) => any) ? P : [];

Tail<[1, 2, 3]> // [2, 3]

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@sindresorhus
Copy link
Owner

What's the real-world use-case?

@rifler
Copy link
Author

rifler commented Oct 21, 2020

for example some of latest release of got brokes many of our types, changed type of context field
lets patch some of the types

import { BeforeRedirectHook as GotBeforeRedirectHook, NormalizedOptions } from 'got';

type PatchedNormalizedOptions = Omit<NormalizedOptions, 'context'> & {
    context?: YourType;
};

type BeforeRedirectHook = (options: PatchedNormalizedOptions, ...args: Tail<Parameters<GotBeforeRedirectHook>>) => ReturnType<GotBeforeRedirectHook>;

here I overrite first argument and use Tail to add original arguments to the end

@sweetliquid
Copy link

type Tail<T extends any[]> = T extends [any, ...infer P] ? P : []

I like this.

@Narretz
Copy link

Narretz commented Sep 23, 2023

I've used tail to create a function signature without the first argument for curried functions.

@sindresorhus
Copy link
Owner

This is accepted. I think Tail is too ambigious. Should be ArrayTail.

We actually already use this internally:

/**
Extracts the type of an array or tuple minus the first element.
*/
export type ArrayTail<TArray extends UnknownArrayOrTuple> = TArray extends readonly [unknown, ...infer TTail] ? TTail : [];

@sindresorhus sindresorhus added help wanted Extra attention is needed type addition labels Sep 25, 2023
@sindresorhus sindresorhus changed the title Proposal: Tail Proposal: ArrayTail Sep 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed type addition
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants