3 ms·
If the return type was Array<Leaf | Aggregator>, wouldn't TypeScript be able to infer the right type U in Array<T>.concat => Array<U> ?
by sephoric 8y ago
If the return type was Array<Leaf | Aggregator>, wouldn't TypeScript be able to infer the right type U in Array<T>.concat => Array<U> ?
- WorldMaker 8y agoNo, because JS concat is an in place operation, Typescript models it as Array<T>.concat() (intentionally leaves it "narrow") rather than Array<T>.concat<U extends T>(). In this case it is the input type that potentially would need to change so that the left hand side was Array<Leaf | Aggregator>. Or as others point out, array spread does widen unlike concat (because it isn't an in-place operation).
- WorldMaker 8y agoApparently concat does not operate in place, my mistake. I've been coding in TS for so long I assumed its definition was correct. https://news.ycombinator.com/item?id=18907771 https://news.ycombinator.com/item?id=18907771 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...