New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ReadonlyArray<T> instead of T[] #2773
Comments
Sounds like a sensible request. Small, focused (separate one per widget) PRs welcomed. |
Could you illustrate this extra effort you're mentioning? I am not sure to get what you are describing here. |
@benouat Sure. Let's take the above example with Now, suppose our app conforms to immutability ideology and uses The first thought is just to perform a type cast:
But we shouldn't do this, since if NGB actually calls any mutating method on that array which is missing in our So, the simplest way I'm aware of is to make a copy of our immutable array with
It would create a mutable copy of our array. |
@pkozlowski-opensource @benouat I haven't checked it out, but I believe it's not specific to |
Just update to the syntax, In the TypeScript 3.4 (released this year, after the original problem creation), the someArray: ReadonlyArray<T> = []; one could simply update existing decclaration with: someArray: readonly T[] = []; as the |
Feature description:
A proposal is to use
ReadonlyArray<T>
instead ofT[]
for input (as seen from NGB side) arrays throughout the publicly visible API, for instance, as a return value type forngbTypeahead
function inNgbTypeahead
:instead of:
It explicitly declares by NGB the immutability of arrays as well as ensures that both groups of developers who use/don't use
ReadonlyArray<T>
would be able to communicate with NGB without extra effort likearray.slice()
before passing to\returning from NGB methods.The above part doesn't introduce a breaking change. We could also consider go further and do the same for output arrays as well to promote immutability in the client code bases.
Versions of Angular, ng-bootstrap and Bootstrap:
ng-bootstrap: 3.3.0
The text was updated successfully, but these errors were encountered: