Allow for entire interface definitions to be marked "readonly" #45535
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
Suggestion
Just interfaces. I wanted to request this specifically since it touches a lot of other things that are under discussion in the issue history here.
π Search Terms
#10725 (comment)
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
From the search term linked to above:
If someone adds another property, it may not be clear that Foo is supposed to be treated as an immutable type.
π Interfaces individually should be marked readonly, any extending of it must designate those restrictions themselves. More in use cases.
π Motivating Example
Python's frozen dataclasses, with better guarantees
π» Use Cases
In the effort to strike a good balance between "immutable" typescript and productivity payolffs, I would suggest two options: atomic readonly interfaces (nothing other than that exact interface is readonly) as well as persistent readonly interfaces (like the kinds you get with an ORM).
An atomic readonly interface is bounded to just the one interface, but without marking itself as persistent only other references remain readonly. This is the default.
An interface which needs mutable properties in B would still be able to do so, with an "abstract override".
This abstract interface pattern can be used to define a stricter "deep" readonly behavior.
Going all out this way gets you the ability to generate warnings/reports of abstract interface usage (and importantly, overrides of those interfaces), as well as any references that appear later on where this override was actually used.
The text was updated successfully, but these errors were encountered: