Yomguithereal / mnemonist

Curated collection of data structures for the JavaScript/TypeScript language.
https://yomguithereal.github.io/mnemonist
MIT License
2.24k stars 92 forks source link

Fix returned TS type on toArray() and consume() methods #181

Closed jerome-benoit closed 2 years ago

jerome-benoit commented 2 years ago

Signed-off-by: Jérôme Benoit jerome.benoit@piment-noir.org

Yomguithereal commented 2 years ago

Hello @jerome-benoit. While I see the obvious reason why this would be better, your PR introduce a type error because the array returned might be from a different class, based on the array constructor that was (sometimes optionally) passed to the structure as argument to shape the underlying storage. So I guess the best way here would be to introduce an optional generic to type this, but I see how this could be cumbersome and I am wondering whether we could find a better solution that does not end up in information erasure through Array<T>.

jerome-benoit commented 2 years ago

Other array types are still array. The best way is to use or in the type: Array<T> | Uint8Array | Uint16Array | Uin32Array | ... or a typeof cast

Yomguithereal commented 2 years ago

Thanks @jerome-benoit. I will merge now. I think we can find a better way through generics but I can't find a way not to bother the user with it so type inference works properly so your solution is a good tradeoff for now I think.

Yomguithereal commented 2 years ago

Published in v0.39.2 on npm