-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Comments
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!
} |
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, 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? |
Yes, appears we still have an issue. I will reopen. |
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]
} |
@jack-williams The example above works with #26676. |
TypeScript Version: 3.1.0-dev.20180802
Search Terms:
Mapped tuple type generic infer array
Code
Expected behavior:
The type of
R1
should bestring | number | boolean
.Actual behavior:
The type of
R1
isnever
.The text was updated successfully, but these errors were encountered: