You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've been using the new readonly properties feature (#6532) and have found it to be a very productive alternative to using getters or getWhatever() functions, or as is often the case, simply doing nothing to enforce read-only properties. However I'm really yearning for the ability to mark constructor parameters as readonly.
In 1.8 a common pattern for me is to write classes where all properties are public and readonly like so:
Right now I'm worried about hitting 2.0 and having to rewrite all my constructors just to take advantage of readonly. Because it's an advantage I definitely want to have.
Leon Yu (@leonyu) Copying the arguments is not what I'm after. I'm asking for exactly the same semantics as readonly. No copying, no runtime checks. Just a shorthand for creating a readonly property, in the same way that adding public to a parameter is a shorthand way to create a public property.
Reacted by Steven Hugg, Martijn Hoekstra, thiloplanz and Kevin Baxmann
Really glad this is implemented. In addition to the increased expressiveness, this allows a fair amount of code currently using accessibility modifiers for the sake of brevity to be written with clearer intent.
Activity
DanielRosenwasser commentedon Mar 19, 2016
So would a
readonlyparameter be treated as aconstwithin the body of its constructor?jahewson commentedon Mar 21, 2016
No, it would simply be a shorthand for creating properties in the manner of
constructor(public foo) { ... }, only we'd get areadonlyproperty.leonyu commentedon Apr 4, 2016
This can probably be implemented using a decorator on the function doing a defensive copy on the arguments. However, that would be a runtime check.
jahewson commentedon Apr 5, 2016
Leon Yu (@leonyu) Copying the arguments is not what I'm after. I'm asking for exactly the same semantics as
readonly. No copying, no runtime checks. Just a shorthand for creating areadonlyproperty, in the same way that addingpublicto a parameter is a shorthand way to create a public property.nbransby commentedon May 4, 2016
+1
RyanCavanaugh commentedon May 9, 2016
Makes a lot of sense. We'll include this along with the first release with
readonlyjahewson commentedon May 10, 2016
Awesome, thanks!
Fix #7590: Allow 'readonly' to be used in constructor parameters
Fix #7590: Allow 'readonly' to be used in constructor parameters
Merge pull request #8555 from Microsoft/readonly_ctr
aluanhaddad commentedon May 23, 2016
Really glad this is implemented. In addition to the increased expressiveness, this allows a fair amount of code currently using accessibility modifiers for the sake of brevity to be written with clearer intent.