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

Mapped tuple types don't infer as arrays in generics #26163

Closed
jscheiny opened this issue Aug 2, 2018 · 5 comments · Fixed by #26517
Closed

Mapped tuple types don't infer as arrays in generics #26163

jscheiny opened this issue Aug 2, 2018 · 5 comments · Fixed by #26517
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@jscheiny
Copy link

jscheiny commented Aug 2, 2018

TypeScript Version: 3.1.0-dev.20180802

Search Terms:

Mapped tuple type generic infer array

Code

type Element<T> = T extends Array<infer U> ? U : never;
type Mapped<T> = { [K in keyof T]: T[K] };

type F<T> = Element<Mapped<T>>;
type R1 = F<[string, number, boolean]>; // never
type R2 = Element<Mapped<[string, number, boolean]>>; // string | number | boolean

Expected behavior:

The type of R1 should be string | number | boolean.

Actual behavior:

The type of R1 is never.

@jscheiny jscheiny changed the title Mapped tuple types don't infer as arrays in generic function return types Mapped tuple types don't infer as arrays in generics Aug 2, 2018
@mhegazy mhegazy added the Bug A bug in TypeScript label Aug 2, 2018
@mhegazy mhegazy added this to the TypeScript 3.1 milestone Aug 2, 2018
@jcalz
Copy link
Contributor

jcalz commented Aug 5, 2018

Yeah, this has bitten me too...

declare function acceptArray(arr: any[]): void;
declare function mapArray<T extends any[]>(arr: T): {[K in keyof T]: T[K]};
function acceptMappedArray<T extends any[]>(arr: T) {
  acceptArray(mapArray(arr)); // error! 
}

@jscheiny
Copy link
Author

jscheiny commented Aug 21, 2018

I just got a chance to try this out, and it works well in situations where the generic type parameter is unconstrained however adding a constraint to this seems to mess things up as below:

type Element<T> = T extends Array<infer U> ? U : never;
type Mapped<T> = { [K in keyof T]: T[K] };

type Unconstrained<T> = Element<Mapped<T>>;
type T1 = Unconstrained<[string, number, boolean]>; // string | number | boolean

type Constrained<T extends any[]> = Element<Mapped<T>>;
type T2 = Constrained<[string, number, boolean]>; // any

In this case, Constrained<T> seems to always return any despite its only difference from Unconstrained<T> being the extends any[] clause. It seems that the type of the constraint is important as can be seen below:

type Unconstrained<T> = Element<Mapped<T>>;
type T1 = Unconstrained<[1, 2, 3]>; // 1 | 2 | 3

type Constrained<T extends number[]> = Element<Mapped<T>>;
type T2 = Constrained<[1, 2, 3]>; // number

Should I re-open this ticket or file a new one?

@ahejlsberg
Copy link
Member

Yes, appears we still have an issue. I will reopen.

@ahejlsberg ahejlsberg reopened this Aug 22, 2018
@ahejlsberg ahejlsberg removed the Fixed A PR has been merged for this issue label Aug 22, 2018
@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Aug 22, 2018
@jack-williams
Copy link
Collaborator

Should the following have also been fixed, or is there another issue at play here?

type ID<T extends any[]> = { [P in keyof T]: T[P] };

interface Test<U extends any[]> {
    (...args: ID<U>): number // A rest parameter must be of an array type. [2370]
}

@ahejlsberg
Copy link
Member

@jack-williams The example above works with #26676.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants