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
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
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:
but it seems like it should just work as is.
Playground Link