TypeScript: inference for `Iterable<Iterable<T>>` is worse than for `Array<Iterable<T>>`

Viewed 15

TypeScript Playground link

// Only difference between concat1 and concat2 is the argument type.
declare function concat1<T>(it: Array<Iterable<T>>): Iterable<T>;
declare function concat2<T>(it: Iterable<Iterable<T>>): Iterable<T>;

type Point = {x: number, y: number};

const a: Array<Point> = [];

// concat1 works as expected
const x1 = concat1([a, a]);
const y1: Iterable<Point> = x1;

// concat2 does not
const x2 = concat2([a, a]); // inferred as Iterable<unknown>
const y2: Iterable<Point> = x2; // TS ERROR: Type 'Iterable<unknown>' is not assignable to type 'Iterable<Point>'.

// concat2 seems to work when we provide an expected type for the expression.
const x3: Iterable<Point> = concat2([a, a]);
const y3: Iterable<Point> = x3;
0 Answers
Related