Skip to content

Mappable tuples with constraints are unwieldy due to lack of covariance #27351

@pelotom

Description

@pelotom

TypeScript Version: 3.1.0-dev.20180925

Search Terms: mappable tuples constraints covariance

I'm trying to make use of the new "mappable tuple and array types" coming in 3.1, and discovered that it's a bit awkward to work with arrays constrained to a particular type.

Code

interface WithFoo<T = any> {
  foo: T;
}

type SelectFoos<A extends WithFoo[]> = { [K in keyof A]: A[K]['foo'] };

Expected behavior:

Should compile.

Actual behavior:

Error: Type '"foo"' cannot be used to index type 'A[K]'..

Notes

It's possible to work around this using a conditional:

type SelectFoos<A extends WithFoo[]> = {
  [K in keyof A]: A[K] extends WithFoo ? A[K]['foo'] : never
};

but it seems like it should just work as is.

Playground Link

Metadata

Metadata

Assignees

Labels

Design LimitationConstraints of the existing architecture prevent this from being fixed

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions