Why does list get converted into iterable? #684
This might be a silly question.
How come when I map over a list it gets converted into an iterable? This means I have to litter my code with
List([1,2]).map(x => x+1).toList().interpose('foo');
Thanks again for the awesome work!
The text was updated successfully, but these errors were encountered:
It doesn't, in your example
Large parts of the type definition can be improved following the merging of microsoft/TypeScript#4910 however the issue with
Whatever the solution here, it raises the question of how we improve the type definition whilst maintaining some degree of backwards compatibility. I can potentially foresee a situation where we end up with multiple typings with suffixes that correspond to TypeScript compiler versions...
In any case, the immediate solution here is to simply assert the type:
(<List<number>>List([1,2]).map(x => x+1)).interpose('foo')
because we know the underlying implementation actually returns a container of the same type,
Breaking apart your first example according to precedence of operators:
List([1,2]) // has type List<number>, inferred from the array of numbers you provide List([1,2]).map(x => x+1) // therefore has type Iterable<number, number>, per previous comments about map signature
The type assertion has the weakest precedence hence these two are equivalent:
<List<number | string>>List([1,2]).map(x => x+1) <List<number | string>>(List([1,2]).map(x => x+1))
which is why you get a compile error in both cases.
Given your map function (which appears to only work on
Some other comments: