Why does list get converted into iterable? #684
Comments
It doesn't, in your example |
Ah, but I get a compile error when using TypeScript: Immutable.List(['foo'])
.map(x => x)
.interpose(5) Error:
|
#634 and microsoft/TypeScript#4967 are related. 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, |
I am using type assertions to work around this, but how come this doesn't work?
|
Or this:
|
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:
|
Thanks @myitcv, that helps! |
Fixed in master, will be released soon |
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
toList
everywhere, like so:Thanks again for the awesome work!
The text was updated successfully, but these errors were encountered: