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

Allow inferring rest element types in conditional types involving tuples #25719

Open
4 tasks done
AlCalzone opened this issue Jul 17, 2018 · 8 comments
Open
4 tasks done
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@AlCalzone
Copy link
Contributor

Search Terms

rest element infer tuple

Suggestion

Currently, inferring single elements of tuple types is possible using the infer keyword:

type FirstArg<T extends any[]> = 
	T extends [infer R, ...any[]] ? R : 
	T extends [] ? undefined : 
	never;
type T1 = FirstArg<[number, 2, 3]>; // number
type T2 = FirstArg<[1, 2, 3]>; // 1
type T3 = FirstArg<["", 2]>; // ""
type T4 = FirstArg<[]>; // undefined

However it is not possible to infer the type of the remaining arguments in one go, except by resorting to functions:

type RestArgs<T extends any[]> = 
	T extends [any, infer R[]] ? R : // this does not work - no way to specify that R should be an array!
	T extends [any] ? []] : 
	never;

// this does
type RestArgs<T extends any[]> =
	((...args: T) => void) extends ((first: any, ...rest: infer S1) => void) ? S1
	: T extends [infer S2] ? []
	: T extends [] ? []
	: never;
type T1 = RestArgs<[1,2,3]>; // [2, 3]
type T2 = RestArgs<[1,2]>; // [2]
type T3 = RestArgs<[1]>; // []
type T4 = RestArgs<[]>; // []

I would like to see the possibility to infer rest types in tuples, e.g. like this (square brackets):

type RestArgs<T extends any[]> = T extends [any, infer R[]] ? R : never;

or like this (3 dots)

type RestArgs<T extends any[]> = T extends [any, infer ...R] ? R : never;

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript / JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. new expression-level syntax)
@jscheiny
Copy link

I just ran into this issue myself and wanted to throw in my two cents about the syntax for how this would behave. Considering how this inference is written for function types and how rest elements of tuples are constructed:

type Tail<T> = T extends (head: any, ...tail: infer U) ? U : never;

type Rest = string[];
type Tuple = [any, ...Rest];

I would suggest the following syntax:

type Tail<T> = T extends [any, ...infer U] ? U : never;

@AlCalzone AlCalzone mentioned this issue Aug 7, 2018
4 tasks
@aleclarson
Copy link

aleclarson commented Oct 27, 2018

The current workaround:

type Tail<T extends any[]> = ((...args: T) => any) extends ((
    _: infer First,
    ...rest: infer Rest
) => any)
    ? T extends any[] ? Rest : ReadonlyArray<Rest[number]>
    : []

Note: Does not work in TypeScript 2.9.x and under.

@darcyparker
Copy link

darcyparker commented Jun 11, 2019

@AlCalzone - Thanks for the examples FirstArg and RestArgs. They inspired these additional examples:

export type Length<T extends any[]> = T extends (infer U)[] & { length: infer L } ? L : never;
export type LengthMinusOne<T extends any[]> = Length<Tail<T>>;
export type LengthPlusOne<T extends any[]> = Length<Cons<any, T>>;
export type Last<T extends any[]> = T[LengthMinusOne<T>];
// Example:
// export type TestLastType = Last<[boolean, string, number]>; //number

Based on the above, it would be great to have something like:

type Last<T extends any[]> = T extends [...any[], infer L] ? L : never;

@smcatala
Copy link

type Tail<A extends any[]> = 
  ((...args: A) => any) extends ((h: any, ...t: infer T) => any) ? T : never

@chocolateboy
Copy link

This works in TypeScript v4 (via variadic tuple types):

type Tail<T extends any[]> = T extends [any, ...infer U] ? U : never;

@ethanresnick
Copy link
Contributor

This seems like it can probably be closed, given the snippet shown above?

@futtetennista
Copy link

Not sure that works but I might be missing something here. For example

type Tail<T extends any[]> = T extends [any, ...infer U] ? U : never;
const xs = ["1", "2", "3"];
type T = Tail<typeof xs>; // never

Playground link

@ethanresnick
Copy link
Contributor

@futtetennista It fails in your example because typeof xs is not a tuple type (it's string[]). Here is an updated version that works:

const xs = ["1", "2", "3"] as const
type Xs = typeof xs
type Tail<T extends readonly any[]> = T extends readonly [any, ...infer U] ? U : never;
type TT = Tail<Xs>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

9 participants