Skip to content

Using a type alias results in a different type than if the alias' value is written out in full when using rest tuples from function parameters #25793

@JakeTunaley

Description

@JakeTunaley

TypeScript Version: 3.1.0-dev.20180717

Search Terms: rest tuples function parameters arguments

Code

// Gets the parameters of a function type as a tuple
type Parameters<T extends (...args: any[]) => any> = T extends (...args: infer U) => any ? U : never;
// Removes the first element from a tuple
type Tail<T extends any[]> = ((...args: T) => any) extends ((head: any, ...tail: infer U) => any) ? U : never;

// Example function type
type MyFunctionType = (foo: number, bar: string) => boolean;

// Note that when we write this type out explicitly, we get the correct result
type Explicit = (...args: Tail<Parameters<MyFunctionType>>) => ReturnType<MyFunctionType>; // (bar: string) => boolean

// But when we try and genericize the above and just pass in the function type, we get a different result
type Bind1<T extends (head: any, ...tail: any[]) => any> = (...args: Tail<Parameters<T>>) => ReturnType<T>;
type Generic = Bind1<MyFunctionType>; // (...args: any[]) => boolean

Expected behavior:
The explicit definition (type Explicit) should result in the same type as the generic definition (type Generic).

Actual behavior:
The generic definition discards the function parameter data.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions